Skip to content

Commit 96db963

Browse files
committed
<fix>[multi]: batch guard NPE quality issues
- UpdateQueryImpl: guard val.getClass() NPE - LogSafeGson: return JsonNull when input is null - HostAllocatorChain: null check completion - VmInstanceVO: use Objects.equals to avoid NPE - SessionManagerImpl: guard null session - VmCapabilitiesJudger: guard null PS type result - CephPSMonBase: guard null self when evicted - CephPSBase: guard null mon.getSelf() - HostBase: guard null self when HostVO deleted - ExternalPSFactory: guard null URI protocol - LocalStorageBase: guard null errorCode.getCause() Resolves: ZSTAC-69300, ZSTAC-69957, ZSTAC-71973, ZSTAC-81294, ZSTAC-70180, ZSTAC-70181, ZSTAC-78309, ZSTAC-78310, ZSTAC-70668, ZSTAC-71909, ZSTAC-80555, ZSTAC-81270, ZSTAC-70101, ZSTAC-72034, ZSTAC-73197, ZSTAC-79921, ZSTAC-81160, ZSTAC-81224, ZSTAC-81805, ZSTAC-72304, ZSTAC-81804, ZSTAC-74898, ZSTAC-69215, ZSTAC-70151, ZSTAC-68933 Change-Id: I910e9b542ecd254fdf7e956f943316988a56a1f9
1 parent cf48ab8 commit 96db963

11 files changed

Lines changed: 36 additions & 10 deletions

File tree

compute/src/main/java/org/zstack/compute/allocator/HostAllocatorChain.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ private void runFlow(AbstractHostAllocatorFlow flow) {
146146
}
147147
} catch (Throwable t) {
148148
logger.warn("unhandled throwable", t);
149-
completion.fail(inerr(ORG_ZSTACK_COMPUTE_ALLOCATOR_10019, t.toString()));
149+
String errMsg = t != null ? t.toString() : "unknown error";
150+
if (completion != null) {
151+
completion.fail(inerr(ORG_ZSTACK_COMPUTE_ALLOCATOR_10019, errMsg));
152+
}
150153
}
151154
}
152155

compute/src/main/java/org/zstack/compute/host/HostBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ public String getName() {
14431443

14441444
@Override
14451445
protected String getDeduplicateString() {
1446-
return String.format("connect-host-%s", self.getUuid());
1446+
return String.format("connect-host-%s", self == null ? "unknown" : self.getUuid());
14471447
}
14481448
});
14491449
}

compute/src/main/java/org/zstack/compute/vm/VmCapabilitiesJudger.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ private void checkPrimaryStorageCapabilities(VmCapabilities capabilities, VmInst
5353
q.add(PrimaryStorageVO_.uuid, SimpleQuery.Op.EQ, rootVolume.getPrimaryStorageUuid());
5454
String type = q.findValue();
5555

56+
if (type == null) {
57+
capabilities.setSupportLiveMigration(false);
58+
capabilities.setSupportVolumeMigration(false);
59+
return;
60+
}
61+
5662
PrimaryStorageType psType = PrimaryStorageType.valueOf(type);
5763

5864
if (vm.getState() != VmInstanceState.Running) {

core/src/main/java/org/zstack/core/db/UpdateQueryImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public UpdateQuery set(SingularAttribute attr, Object val) {
5454
@Override
5555
public UpdateQuery condAnd(SingularAttribute attr, Op op, Object val) {
5656
if ((op == Op.IN || op == Op.NOT_IN) && !(val instanceof Collection)) {
57-
throw new CloudRuntimeException(String.format("for operation IN or NOT IN, a Collection value is expected, but %s got", val.getClass()));
57+
throw new CloudRuntimeException(String.format("for operation IN or NOT IN, a Collection value is expected, but %s got",
58+
val == null ? "null" : val.getClass()));
5859
}
5960

6061
Cond cond = new Cond();

core/src/main/java/org/zstack/core/log/LogSafeGson.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ private static <T> JsonSerializer<T> getSerializer() {
208208
}
209209

210210
public static JsonElement toJsonElement(Object o) {
211+
if (o == null) {
212+
return JsonNull.INSTANCE;
213+
}
211214
return logSafeGson.toJsonTree(o, getGsonType(o.getClass()));
212215
}
213216

header/src/main/java/org/zstack/header/vm/VmAbnormalLifeCycleStruct.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ boolean match(VmAbnormalLifeCycleStruct struct) {
7272
boolean match(VmAbnormalLifeCycleStruct struct) {
7373
return struct.getOriginalState() == VmInstanceState.Paused
7474
&& struct.getCurrentState() == VmInstanceState.Stopped
75-
&& struct.getCurrentHostUuid().equals(struct.getOriginalHostUuid());
75+
&& Objects.equals(struct.getCurrentHostUuid(), struct.getOriginalHostUuid());
7676
}
7777
},
7878
VmMigrateToAnotherHost {

identity/src/main/java/org/zstack/identity/AuthorizationManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ public APIMessage intercept(APIMessage msg) throws ApiMessageInterceptionExcepti
116116
session = evaluateSession(msg);
117117
}
118118

119+
if (session == null) {
120+
throw new ApiMessageInterceptionException(err(ORG_ZSTACK_IDENTITY_10012, IdentityErrors.INVALID_SESSION,
121+
"evaluated session is null for message[%s]", msg.getMessageName()));
122+
}
123+
119124
logger.trace(String.format("authorizing message[%s] with user[accountUuid:%s, uuid:%s] session",
120125
msg.getMessageName(),
121126
session.getAccountUuid(),

plugin/ceph/src/main/java/org/zstack/storage/ceph/primary/CephPrimaryStorageBase.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3974,15 +3974,17 @@ public void done() {
39743974
mon.connect(new Completion(releaseLock) {
39753975
@Override
39763976
public void success() {
3977+
String monUuid = mon.getSelf() == null ? "unknown" : mon.getSelf().getUuid();
39773978
logger.debug(String.format("successfully reconnected the mon[uuid:%s] of the ceph primary" +
3978-
" storage[uuid:%s, name:%s]", mon.getSelf().getUuid(), self.getUuid(), self.getName()));
3979+
" storage[uuid:%s, name:%s]", monUuid, self.getUuid(), self.getName()));
39793980
releaseLock.done();
39803981
}
39813982

39823983
@Override
39833984
public void fail(ErrorCode errorCode) {
3985+
String monUuid = mon.getSelf() == null ? "unknown" : mon.getSelf().getUuid();
39843986
logger.warn(String.format("failed to reconnect the mon[uuid:%s] server of the ceph primary" +
3985-
" storage[uuid:%s, name:%s], %s", mon.getSelf().getUuid(), self.getUuid(), self.getName(), errorCode));
3987+
" storage[uuid:%s, name:%s], %s", monUuid, self.getUuid(), self.getName(), errorCode));
39863988
releaseLock.done();
39873989
}
39883990
});

plugin/ceph/src/main/java/org/zstack/storage/ceph/primary/CephPrimaryStorageMonBase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void fail(ErrorCode errorCode) {
141141

142142
@Override
143143
public String getName() {
144-
return String.format("connect-ceph-primary-storage-mon-%s", self.getUuid());
144+
return String.format("connect-ceph-primary-storage-mon-%s", self == null ? "unknown" : self.getUuid());
145145
}
146146
});
147147
}
@@ -420,7 +420,7 @@ public void fail(ErrorCode errorCode) {
420420

421421
@Override
422422
public String getName() {
423-
return String.format("ping-ceph-primary-storage-%s", self.getUuid());
423+
return String.format("ping-ceph-primary-storage-%s", self == null ? "unknown" : self.getUuid());
424424
}
425425
});
426426
}

plugin/localstorage/src/main/java/org/zstack/storage/primary/local/LocalStorageAllocatorFactory.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,12 @@ private String getHostUuidFromAllocateMsg(AllocatePrimaryStorageSpaceMsg msg) {
385385
throw new OperationFailureException(
386386
argerr(ORG_ZSTACK_STORAGE_PRIMARY_LOCAL_10023, "invalid uri, correct example is file://$URL;hostUuid://$HOSTUUID or volume://$VOLUMEUUID "));
387387
}
388-
hostUuid = uriParsers.get(protocol).parseUri(msg.getRequiredInstallUri()).hostUuid;
388+
AbstractUriParser parser = uriParsers.get(protocol);
389+
if (parser == null) {
390+
throw new OperationFailureException(
391+
argerr(ORG_ZSTACK_STORAGE_PRIMARY_LOCAL_10023, "unsupported protocol[%s] in uri[%s]", protocol, msg.getRequiredInstallUri()));
392+
}
393+
hostUuid = parser.parseUri(msg.getRequiredInstallUri()).hostUuid;
389394
}
390395

391396
if (hostUuid != null) {

0 commit comments

Comments
 (0)