Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.zstack.header.storage.primary;

import org.zstack.header.message.APIEvent;
import org.zstack.header.rest.RestResponse;

import java.util.Collections;

@RestResponse(allTo = "inventory")
public class APITakeoverPrimaryStorageEvent extends APIEvent {
private PrimaryStorageInventory inventory;

public APITakeoverPrimaryStorageEvent() {
}

public APITakeoverPrimaryStorageEvent(String apiId) {
super(apiId);
}

public PrimaryStorageInventory getInventory() {
return inventory;
}

public void setInventory(PrimaryStorageInventory inventory) {
this.inventory = inventory;
}

public static APITakeoverPrimaryStorageEvent __example__() {
APITakeoverPrimaryStorageEvent event = new APITakeoverPrimaryStorageEvent();

PrimaryStorageInventory ps = new PrimaryStorageInventory();
ps.setName("PS1");
ps.setUrl("/zstack_ps");
ps.setType("LocalStorage");
ps.setAttachedClusterUuids(Collections.singletonList(uuid()));

event.setInventory(ps);
return event;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.zstack.header.storage.primary;

import org.springframework.http.HttpMethod;
import org.zstack.header.message.APIMessage;
import org.zstack.header.message.APIParam;
import org.zstack.header.rest.RestRequest;

@RestRequest(
path = "/primary-storage/{uuid}/takeover",
responseClass = APITakeoverPrimaryStorageEvent.class,
method = HttpMethod.PUT,
isAction = true
)
public class APITakeoverPrimaryStorageMsg extends APIMessage implements PrimaryStorageMessage {
@APIParam(resourceType = PrimaryStorageVO.class)
private String uuid;

@Override
public String getPrimaryStorageUuid() {
return uuid;
}

public String getUuid() {
return uuid;
}

public void setUuid(String uuid) {
this.uuid = uuid;
}

public static APITakeoverPrimaryStorageMsg __example__() {
APITakeoverPrimaryStorageMsg msg = new APITakeoverPrimaryStorageMsg();

msg.setUuid(uuid(PrimaryStorageVO.class));

return msg;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.zstack.header.storage.primary;

import org.zstack.header.message.NeedReplyMessage;

public class TakeoverPrimaryStorageMsg extends NeedReplyMessage implements PrimaryStorageMessage {
private String primaryStorageUuid;

@Override
public String getPrimaryStorageUuid() {
return primaryStorageUuid;
}

public void setPrimaryStorageUuid(String primaryStorageUuid) {
this.primaryStorageUuid = primaryStorageUuid;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.zstack.header.storage.primary;

import org.zstack.header.message.MessageReply;

public class TakeoverPrimaryStorageReply extends MessageReply {
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ public void setNewAdded(boolean newAdded) {

protected abstract void handle(GetVolumeSnapshotEncryptedOnPrimaryStorageMsg msg);

protected void takeoverHook(Completion completion){
completion.success();
}

public PrimaryStorageBase(PrimaryStorageVO self) {
this.self = self;
}
Expand Down Expand Up @@ -603,6 +607,22 @@ public void fail(ErrorCode errorCode) {
});
}

protected void handle(TakeoverPrimaryStorageMsg msg) {
TakeoverPrimaryStorageReply reply = new TakeoverPrimaryStorageReply();
doTakeover(new ConnectParam(), new Completion(msg) {
@Override
public void success() {
bus.reply(msg, reply);
}

@Override
public void fail(ErrorCode errorCode) {
reply.setError(errorCode);
bus.reply(msg, reply);
}
});
}

private void handle(ChangePrimaryStorageStatusMsg msg) {
changeStatus(PrimaryStorageStatus.valueOf(msg.getStatus()));
ChangePrimaryStorageStatusReply reply = new ChangePrimaryStorageStatusReply();
Expand Down Expand Up @@ -695,6 +715,51 @@ public String getName() {
});
}

private void doTakeover(ConnectParam param, final Completion completion) {
thdf.chainSubmit(new ChainTask(completion) {
@Override
public String getSyncSignature() {
return getSyncId();
}

@Override
public void run(SyncTaskChain chain) {
takeoverHook(new Completion(chain, completion) {
@Override
public void success() {
self = dbf.reload(self);
logger.debug(String.format("successfully reload primary storage[uuid:%s]", self.getUuid()));

tracker.track(self.getUuid());

completion.success();
chain.next();
}

@Override
public void fail(ErrorCode errorCode) {
tracker.track(self.getUuid());

self = dbf.reload(self);
if (changeStatus(PrimaryStorageStatus.Disconnected) && !errorCode.isError(PrimaryStorageErrors.DISCONNECTED)) {
fireDisconnectedCanonicalEvent(errorCode);
}

logger.debug(String.format("failed to connect primary storage[uuid:%s], %s", self.getUuid(), errorCode));

completion.fail(errorCode);
chain.next();
}
});
}

@Override
public String getName() {
return String.format("reconnect-primary-storage-%s", self.getUuid());
Comment on lines +748 to +758
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

日志消息和任务名称与操作不一致

复制自 doConnect 方法时未更新相关字符串:

  • Line 749: 日志消息显示 "failed to connect" 但实际是 reload 操作
  • Line 759: 任务名称为 "reconnect-primary-storage" 应为 "reload-primary-storage"
✏️ 修复建议
-                        logger.debug(String.format("failed to connect primary storage[uuid:%s], %s", self.getUuid(), errorCode));
+                        logger.debug(String.format("failed to reload primary storage[uuid:%s], %s", self.getUuid(), errorCode));

                         completion.fail(errorCode);
                         chain.next();
                     }
                 });
             }

             `@Override`
             public String getName() {
-                return String.format("reconnect-primary-storage-%s", self.getUuid());
+                return String.format("reload-primary-storage-%s", self.getUuid());
             }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
logger.debug(String.format("failed to connect primary storage[uuid:%s], %s", self.getUuid(), errorCode));
completion.fail(errorCode);
chain.next();
}
});
}
@Override
public String getName() {
return String.format("reconnect-primary-storage-%s", self.getUuid());
logger.debug(String.format("failed to reload primary storage[uuid:%s], %s", self.getUuid(), errorCode));
completion.fail(errorCode);
chain.next();
}
});
}
`@Override`
public String getName() {
return String.format("reload-primary-storage-%s", self.getUuid());
}
🤖 Prompt for AI Agents
In `@storage/src/main/java/org/zstack/storage/primary/PrimaryStorageBase.java`
around lines 749 - 759, Update the incorrect connect-related strings copied into
the reload flow in PrimaryStorageBase: change the logger message in the reload
handler (the debug call that currently says "failed to connect primary
storage[uuid:%s], %s") to reflect "failed to reload primary storage" and update
the anonymous task name returned by getName() (currently returning
"reconnect-primary-storage-%s") to "reload-primary-storage-%s" so both message
and task name correctly reference the reload operation in the doReload flow.

}
});
}

private void handle(final ConnectPrimaryStorageMsg msg) {
final ConnectPrimaryStorageReply reply = new ConnectPrimaryStorageReply();

Expand Down Expand Up @@ -1397,6 +1462,27 @@ public void run(MessageReply reply) {
});
}

protected void handle(APITakeoverPrimaryStorageMsg msg) {
final APITakeoverPrimaryStorageEvent evt = new APITakeoverPrimaryStorageEvent(msg.getId());

TakeoverPrimaryStorageMsg rmsg = new TakeoverPrimaryStorageMsg();
rmsg.setPrimaryStorageUuid(msg.getPrimaryStorageUuid());
bus.makeTargetServiceIdByResourceUuid(rmsg, PrimaryStorageConstant.SERVICE_ID, rmsg.getPrimaryStorageUuid());
bus.send(rmsg, new CloudBusCallBack(msg) {
@Override
public void run(MessageReply reply) {
if (!reply.isSuccess()) {
evt.setError(reply.getError());
} else {
self = dbf.reload(self);
evt.setInventory(getSelfInventory());
}

bus.publish(evt);
}
});
}

// don't use chainTask for this method, the sub-sequential DetachPrimaryStorageFromClusterMsg
// is in the queue
protected void handle(final APIDetachPrimaryStorageFromClusterMsg msg) {
Expand Down