}
}
// check stateModelDef exists and get initial state
StateModelDefId stateModelDef = idealState.getStateModelDefId();
StateModelDefinition stateModel =
accessor.getProperty(keyBuilder.stateModelDef(stateModelDef.stringify()));
if (stateModel == null) {
throw new HelixException("Can't reset state for " + resourceName + "/" + partitionNames
+ " on " + instanceName + ", because " + stateModelDef + " is NOT found");
}
// check there is no pending messages for the partitions exist
List<Message> messages = accessor.getChildValues(keyBuilder.messages(instanceName));
for (Message message : messages) {
if (!MessageType.STATE_TRANSITION.toString().equalsIgnoreCase(message.getMsgType())
|| !sessionId.equals(message.getTypedTgtSessionId())
|| !resourceName.equals(message.getResourceId().stringify())
|| !resetPartitionNames.contains(message.getPartitionId().stringify())) {
continue;
}
throw new HelixException("Can't reset state for " + resourceName + "/" + partitionNames
+ " on " + instanceName + ", because a pending message exists: " + message);
}
String adminName = null;
try {
adminName = InetAddress.getLocalHost().getCanonicalHostName() + "-ADMIN";
} catch (UnknownHostException e) {
// can ignore it
logger.info("Unable to get host name. Will set it to UNKNOWN, mostly ignorable", e);
adminName = "UNKNOWN";
}
List<Message> resetMessages = new ArrayList<Message>();
List<PropertyKey> messageKeys = new ArrayList<PropertyKey>();
for (String partitionName : resetPartitionNames) {
// send ERROR to initialState message
MessageId msgId = MessageId.from(UUID.randomUUID().toString());
Message message = new Message(MessageType.STATE_TRANSITION, msgId);
message.setSrcName(adminName);
message.setTgtName(instanceName);
message.setMsgState(MessageState.NEW);
message.setPartitionId(PartitionId.from(partitionName));
message.setResourceId(ResourceId.from(resourceName));
message.setTgtSessionId(sessionId);
message.setStateModelDef(stateModelDef);
message.setFromState(State.from(HelixDefinedState.ERROR.toString()));
message.setToState(stateModel.getTypedInitialState());
message.setStateModelFactoryId(idealState.getStateModelFactoryId());
resetMessages.add(message);
messageKeys.add(keyBuilder.message(instanceName, message.getId()));
}