use of org.apache.helix.ZNRecord in project helix by apache.
the class ServerContext method getDataAccssor.
public HelixDataAccessor getDataAccssor(String clusterName) {
synchronized (_helixDataAccessorPool) {
if (!_helixDataAccessorPool.containsKey(clusterName)) {
ZkBaseDataAccessor<ZNRecord> baseDataAccessor = new ZkBaseDataAccessor<>(getZkClient());
_helixDataAccessorPool.put(clusterName, new ZKHelixDataAccessor(clusterName, InstanceType.ADMINISTRATOR, baseDataAccessor));
}
return _helixDataAccessorPool.get(clusterName);
}
}
use of org.apache.helix.ZNRecord in project helix by apache.
the class FileStoreStateModel method onBecomeMasterFromSlave.
/**
* When the node becomes master, it will start accepting writes and increments
* the epoch and starts logging the changes in a file
* @param message
* @param context
* @throws Exception
*/
@Transition(from = "SLAVE", to = "MASTER")
public void onBecomeMasterFromSlave(final Message message, NotificationContext context) throws Exception {
replicator.stop();
System.out.println(_serverId + " transitioning from " + message.getFromState() + " to " + message.getToState() + " for " + _partition);
ZkHelixPropertyStore<ZNRecord> helixPropertyStore = context.getManager().getHelixPropertyStore();
String checkpointDirPath = instanceConfig.getRecord().getSimpleField("check_point_dir");
CheckpointFile checkpointFile = new CheckpointFile(checkpointDirPath);
final ChangeRecord lastRecordProcessed = checkpointFile.findLastRecordProcessed();
DataUpdater<ZNRecord> updater = new HighWaterMarkUpdater(message, lastRecordProcessed);
helixPropertyStore.update("/TRANSACTION_ID_METADATA" + "/" + message.getResourceName(), updater, AccessOption.PERSISTENT);
Stat stat = new Stat();
;
ZNRecord znRecord = helixPropertyStore.get("/TRANSACTION_ID_METADATA" + "/" + message.getResourceName(), stat, AccessOption.PERSISTENT);
int startGen = Integer.parseInt(znRecord.getSimpleField("currentGen"));
int startSeq = Integer.parseInt(znRecord.getSimpleField("currentGenStartSeq"));
String fileStoreDir = instanceConfig.getRecord().getSimpleField("file_store_dir");
String changeLogDir = instanceConfig.getRecord().getSimpleField("change_log_dir");
generator = new ChangeLogGenerator(changeLogDir, startGen, startSeq);
// To indicate that we need callbacks for changes that happen starting now
long now = System.currentTimeMillis();
service = new FileSystemWatchService(fileStoreDir, now, generator);
service.start();
System.out.println(_serverId + " transitioned from " + message.getFromState() + " to " + message.getToState() + " for " + _partition);
}
use of org.apache.helix.ZNRecord in project helix by apache.
the class HelixCustomCodeRunner method start.
/**
* This method will be invoked when there is a change in any subscribed
* notificationTypes
* @throws Exception
*/
public void start() throws Exception {
if (_callback == null || _notificationTypes == null || _notificationTypes.size() == 0 || _resourceName == null) {
throw new IllegalArgumentException("Require callback | notificationTypes | resourceName");
}
LOG.info("Register participantLeader on " + _notificationTypes + " using " + _resourceName);
_stateModelFty = new GenericLeaderStandbyStateModelFactory(_callback, _notificationTypes);
StateMachineEngine stateMach = _manager.getStateMachineEngine();
stateMach.registerStateModelFactory(LEADER_STANDBY, _stateModelFty, _resourceName);
ZkClient zkClient = null;
try {
// manually add ideal state for participant leader using LeaderStandby
// model
zkClient = new ZkClient(_zkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
HelixDataAccessor accessor = new ZKHelixDataAccessor(_manager.getClusterName(), new ZkBaseDataAccessor<ZNRecord>(zkClient));
Builder keyBuilder = accessor.keyBuilder();
IdealState idealState = new IdealState(_resourceName);
idealState.setRebalanceMode(RebalanceMode.SEMI_AUTO);
idealState.setReplicas(IdealState.IdealStateConstants.ANY_LIVEINSTANCE.toString());
idealState.setNumPartitions(1);
idealState.setStateModelDefRef(LEADER_STANDBY);
idealState.setStateModelFactoryName(_resourceName);
List<String> prefList = new ArrayList<String>(Arrays.asList(IdealState.IdealStateConstants.ANY_LIVEINSTANCE.toString()));
idealState.getRecord().setListField(_resourceName + "_0", prefList);
List<String> idealStates = accessor.getChildNames(keyBuilder.idealStates());
while (idealStates == null || !idealStates.contains(_resourceName)) {
accessor.setProperty(keyBuilder.idealStates(_resourceName), idealState);
idealStates = accessor.getChildNames(keyBuilder.idealStates());
}
LOG.info("Set idealState for participantLeader:" + _resourceName + ", idealState:" + idealState);
} finally {
if (zkClient != null && zkClient.getConnection() != null) {
zkClient.close();
}
}
}
use of org.apache.helix.ZNRecord in project helix by apache.
the class ScheduledTaskStateModel method onBecomeCompletedFromOffline.
@Transition(to = "COMPLETED", from = "OFFLINE")
public void onBecomeCompletedFromOffline(Message message, NotificationContext context) throws InterruptedException {
logger.info(_partitionKey + " onBecomeCompletedFromOffline");
// Construct the inner task message from the mapfields of scheduledTaskQueue resource group
Map<String, String> messageInfo = message.getRecord().getMapField(Message.Attributes.INNER_MESSAGE.toString());
ZNRecord record = new ZNRecord(_partitionKey);
record.getSimpleFields().putAll(messageInfo);
Message taskMessage = new Message(record);
if (logger.isDebugEnabled()) {
logger.debug(taskMessage.getRecord().getSimpleFields().toString());
}
MessageHandler handler = _executor.createMessageHandler(taskMessage, new NotificationContext(null));
if (handler == null) {
throw new HelixException("Task message " + taskMessage.getMsgType() + " handler not found, task id " + _partitionKey);
}
// Invoke the internal handler to complete the task
handler.handleMessage();
logger.info(_partitionKey + " onBecomeCompletedFromOffline completed");
}
use of org.apache.helix.ZNRecord in project helix by apache.
the class ZKPathDataDumpTask method dump.
void dump(BaseDataAccessor<ZNRecord> accessor, String ancestorPath, long threshold, int maxLeafCount) {
List<String> leafPaths = scanPath(accessor, ancestorPath);
if (leafPaths.isEmpty()) {
return;
}
Stat[] stats = accessor.getStats(leafPaths, 0);
List<String> dumpPaths = Lists.newArrayList();
long now = System.currentTimeMillis();
for (int i = 0; i < stats.length; i++) {
Stat stat = stats[i];
if ((stats.length > maxLeafCount) || ((now - stat.getMtime()) > threshold)) {
dumpPaths.add(leafPaths.get(i));
}
}
if (!dumpPaths.isEmpty()) {
LOG.info("Dump statusUpdates and errors records for paths: " + dumpPaths);
List<ZNRecord> dumpRecords = accessor.get(dumpPaths, null, 0);
for (ZNRecord record : dumpRecords) {
if (record != null) {
LOG.info(new String(_jsonSerializer.serialize(record)));
}
}
// clean up
accessor.remove(dumpPaths, 0);
LOG.info("Remove statusUpdates and errors records for paths: " + dumpPaths);
}
}
Aggregations