use of com.hazelcast.core.HazelcastException in project orientdb by orientechnologies.
the class ODistributedStorage method readRecord.
public OStorageOperationResult<ORawBuffer> readRecord(final ORecordId iRecordId, final String iFetchPlan, final boolean iIgnoreCache, final boolean prefetchRecords, final ORecordCallback<ORawBuffer> iCallback) {
final ORawBuffer memCopy = localDistributedDatabase.getRecordIfLocked(iRecordId);
if (memCopy != null)
return new OStorageOperationResult<ORawBuffer>(memCopy);
try {
final String clusterName = getClusterNameByRID(iRecordId);
final ODistributedConfiguration dbCfg = distributedConfiguration;
final List<String> nodes = dbCfg.getServers(clusterName, null);
final int availableNodes = nodes.size();
// CHECK IF LOCAL NODE OWNS THE DATA AND READ-QUORUM = 1: GET IT LOCALLY BECAUSE IT'S FASTER
final String localNodeName = dManager.getLocalNodeName();
if (nodes.isEmpty() || nodes.contains(dManager.getLocalNodeName()) && dbCfg.getReadQuorum(clusterName, availableNodes, localNodeName) <= 1) {
// DON'T REPLICATE
return (OStorageOperationResult<ORawBuffer>) OScenarioThreadLocal.executeAsDistributed(new Callable() {
@Override
public Object call() throws Exception {
return wrapped.readRecord(iRecordId, iFetchPlan, iIgnoreCache, prefetchRecords, iCallback);
}
});
}
// DISTRIBUTE IT
final ODistributedResponse response = dManager.sendRequest(getName(), Collections.singleton(clusterName), nodes, new OReadRecordTask(iRecordId), dManager.getNextMessageIdCounter(), EXECUTION_MODE.RESPONSE, null, null);
final Object dResult = response != null ? response.getPayload() : null;
if (dResult instanceof ONeedRetryException)
throw (ONeedRetryException) dResult;
else if (dResult instanceof Exception)
throw OException.wrapException(new ODistributedException("Error on execution distributed read record"), (Exception) dResult);
return new OStorageOperationResult<ORawBuffer>((ORawBuffer) dResult);
} catch (ONeedRetryException e) {
// PASS THROUGH
throw e;
} catch (HazelcastInstanceNotActiveException e) {
throw new OOfflineNodeException("Hazelcast instance is not available");
} catch (HazelcastException e) {
throw new OOfflineNodeException("Hazelcast instance is not available");
} catch (Exception e) {
handleDistributedException("Cannot route read record operation for %s to the distributed node", e, iRecordId);
// UNREACHABLE
return null;
}
}
use of com.hazelcast.core.HazelcastException in project orientdb by orientechnologies.
the class ODistributedStorage method updateRecord.
@Override
public OStorageOperationResult<Integer> updateRecord(final ORecordId iRecordId, final boolean updateContent, final byte[] iContent, final int iVersion, final byte iRecordType, final int iMode, final ORecordCallback<Integer> iCallback) {
resetLastValidBackup();
if (OScenarioThreadLocal.INSTANCE.isRunModeDistributed()) {
// ALREADY DISTRIBUTED
return wrapped.updateRecord(iRecordId, updateContent, iContent, iVersion, iRecordType, iMode, iCallback);
}
checkLocalNodeIsAvailable();
final ODistributedConfiguration dbCfg = distributedConfiguration;
final String clusterName = getClusterNameByRID(iRecordId);
final String localNodeName = dManager.getLocalNodeName();
checkWriteQuorum(dbCfg, clusterName, localNodeName);
try {
checkNodeIsMaster(localNodeName, dbCfg);
final List<String> nodes = dbCfg.getServers(clusterName, null);
if (nodes.isEmpty())
// NO REPLICATION: EXECUTE IT LOCALLY
return wrapped.updateRecord(iRecordId, updateContent, iContent, iVersion, iRecordType, iMode, iCallback);
final Set<String> clusterNames = Collections.singleton(clusterName);
Boolean executionModeSynch = dbCfg.isExecutionModeSynchronous(clusterName);
if (executionModeSynch == null)
executionModeSynch = iMode == 0;
final boolean syncMode = executionModeSynch;
return (OStorageOperationResult<Integer>) executeRecordOperationInLock(syncMode, iRecordId, new OCallable<Object, OCallable<Void, ODistributedRequestId>>() {
@Override
public Object call(OCallable<Void, ODistributedRequestId> unlockCallback) {
final OUpdateRecordTask task = new OUpdateRecordTask(iRecordId, iContent, iVersion, iRecordType);
final OStorageOperationResult<Integer> localResult;
final boolean executedLocally = nodes.contains(localNodeName);
if (executedLocally) {
// EXECUTE ON LOCAL NODE FIRST
try {
// LOAD CURRENT RECORD
task.checkRecordExists();
localResult = (OStorageOperationResult<Integer>) OScenarioThreadLocal.executeAsDistributed(new Callable() {
@Override
public Object call() throws Exception {
task.setLastLSN(wrapped.getLSN());
return wrapped.updateRecord(iRecordId, updateContent, iContent, iVersion, iRecordType, iMode, iCallback);
}
});
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw OException.wrapException(new ODistributedException("Cannot delete record " + iRecordId), e);
}
nodes.remove(localNodeName);
} else
localResult = null;
if (nodes.isEmpty()) {
unlockCallback.call(null);
if (!executedLocally)
throw new ODistributedException("Cannot execute distributed update on record " + iRecordId + " because no nodes are available");
} else {
final Integer localResultPayload = localResult != null ? localResult.getResult() : null;
if (syncMode || localResult == null) {
// REPLICATE IT
try {
final ODistributedResponse dResponse = dManager.sendRequest(getName(), clusterNames, nodes, task, dManager.getNextMessageIdCounter(), EXECUTION_MODE.RESPONSE, localResultPayload, unlockCallback);
final Object payload = dResponse.getPayload();
if (payload instanceof Exception) {
if (payload instanceof ORecordNotFoundException) {
// REPAIR THE RECORD IMMEDIATELY
localDistributedDatabase.getDatabaseRepairer().enqueueRepairRecord((ORecordId) ((ORecordNotFoundException) payload).getRid());
}
executeUndoOnLocalServer(dResponse.getRequestId(), task);
if (payload instanceof ONeedRetryException)
throw (ONeedRetryException) payload;
throw OException.wrapException(new ODistributedException("Error on execution distributed update record"), (Exception) payload);
}
// UPDATE LOCALLY
return new OStorageOperationResult<Integer>((Integer) payload);
} catch (RuntimeException e) {
executeUndoOnLocalServer(null, task);
throw e;
} catch (Exception e) {
executeUndoOnLocalServer(null, task);
ODatabaseException.wrapException(new ODistributedException("Cannot execute distributed update record"), e);
}
}
// ASYNCHRONOUS CALL: EXECUTE LOCALLY AND THEN DISTRIBUTE
asynchronousExecution(new OAsynchDistributedOperation(getName(), Collections.singleton(clusterName), nodes, task, dManager.getNextMessageIdCounter(), localResultPayload, unlockCallback, null));
}
return localResult;
}
});
} catch (ONeedRetryException e) {
localDistributedDatabase.getDatabaseRepairer().enqueueRepairRecord(iRecordId);
// PASS THROUGH
throw e;
} catch (HazelcastInstanceNotActiveException e) {
throw new OOfflineNodeException("Hazelcast instance is not available");
} catch (HazelcastException e) {
throw new OOfflineNodeException("Hazelcast instance is not available");
} catch (Exception e) {
localDistributedDatabase.getDatabaseRepairer().enqueueRepairRecord(iRecordId);
handleDistributedException("Cannot route UPDATE_RECORD operation for %s to the distributed node", e, iRecordId);
// UNREACHABLE
return null;
}
}
use of com.hazelcast.core.HazelcastException in project neo4j by neo4j.
the class HazelcastCoreTopologyService method createHazelcastInstance.
private HazelcastInstance createHazelcastInstance() {
System.setProperty(WAIT_SECONDS_BEFORE_JOIN.getName(), "1");
JoinConfig joinConfig = new JoinConfig();
joinConfig.getMulticastConfig().setEnabled(false);
joinConfig.getAwsConfig().setEnabled(false);
TcpIpConfig tcpIpConfig = joinConfig.getTcpIpConfig();
tcpIpConfig.setEnabled(true);
List<AdvertisedSocketAddress> initialMembers = config.get(CausalClusteringSettings.initial_discovery_members);
for (AdvertisedSocketAddress address : initialMembers) {
tcpIpConfig.addMember(address.toString());
}
Setting<ListenSocketAddress> discovery_listen_address = CausalClusteringSettings.discovery_listen_address;
ListenSocketAddress hazelcastAddress = config.get(discovery_listen_address);
InterfacesConfig interfaces = new InterfacesConfig();
interfaces.addInterface(hazelcastAddress.getHostname());
NetworkConfig networkConfig = new NetworkConfig();
networkConfig.setInterfaces(interfaces);
networkConfig.setPort(hazelcastAddress.getPort());
networkConfig.setJoin(joinConfig);
networkConfig.setPortAutoIncrement(false);
com.hazelcast.config.Config c = new com.hazelcast.config.Config();
c.setProperty(OPERATION_CALL_TIMEOUT_MILLIS.getName(), String.valueOf(10_000));
c.setProperty(MERGE_NEXT_RUN_DELAY_SECONDS.getName(), "10");
c.setProperty(MERGE_FIRST_RUN_DELAY_SECONDS.getName(), "10");
c.setProperty(INITIAL_MIN_CLUSTER_SIZE.getName(), String.valueOf(minimumClusterSizeThatCanTolerateOneFaultForExpectedClusterSize()));
c.setProperty(LOGGING_TYPE.getName(), "none");
c.setNetworkConfig(networkConfig);
MemberAttributeConfig memberAttributeConfig = HazelcastClusterTopology.buildMemberAttributesForCore(myself, config);
c.setMemberAttributeConfig(memberAttributeConfig);
logConnectionInfo(initialMembers);
JobScheduler.JobHandle logJob = scheduler.schedule("HazelcastHealth", HAZELCAST_IS_HEALTHY_TIMEOUT_MS, () -> log.warn("The server has not been able to connect in a timely fashion to the " + "cluster. Please consult the logs for more details. Rebooting the server may " + "solve the problem."));
try {
hazelcastInstance = Hazelcast.newHazelcastInstance(c);
logJob.cancel(true);
} catch (HazelcastException e) {
String errorMessage = String.format("Hazelcast was unable to start with setting: %s = %s", discovery_listen_address.name(), config.get(discovery_listen_address));
userLog.error(errorMessage);
log.error(errorMessage, e);
throw new RuntimeException(e);
}
List<String> groups = config.get(CausalClusteringSettings.server_groups);
refreshGroups(hazelcastInstance, myself.getUuid().toString(), groups);
return hazelcastInstance;
}
use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.
the class SetCommandProcessor method handle.
/**
* "set" means "store this data".
* <p/>
* "add" means "store this data, but only if the server *doesn't* already
* hold data for this key".
* <p/>
* "replace" means "store this data, but only if the server *does*
* already hold data for this key".
* <p/>
* <p/>
* After sending the command line and the data block the client awaits
* the reply, which may be:
* <p/>
* - "STORED\r\n", to indicate success.
* <p/>
* - "NOT_STORED\r\n" to indicate the data was not stored, but not
* because of an error. This normally means that either that the
* condition for an "add" or a "replace" command wasn't met, or that the
* item is in a delete queue (see the "delete" command below).
*/
public void handle(SetCommand setCommand) {
String key = null;
try {
key = URLDecoder.decode(setCommand.getKey(), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new HazelcastException(e);
}
String mapName = DEFAULT_MAP_NAME;
int index = key.indexOf(':');
if (index != -1) {
mapName = MAP_NAME_PRECEDER + key.substring(0, index);
key = key.substring(index + 1);
}
Object value = new MemcacheEntry(setCommand.getKey(), setCommand.getValue(), setCommand.getFlag());
int ttl = textCommandService.getAdjustedTTLSeconds(setCommand.getExpiration());
textCommandService.incrementSetCount();
if (SET == setCommand.getType()) {
textCommandService.put(mapName, key, value, ttl);
setCommand.setResponse(TextCommandConstants.STORED);
} else if (ADD == setCommand.getType()) {
addCommandType(setCommand, mapName, key, value, ttl);
} else if (REPLACE == setCommand.getType()) {
replaceCommandType(setCommand, mapName, key, value, ttl);
} else if (APPEND == setCommand.getType()) {
appendCommandType(setCommand, mapName, key, ttl);
} else if (PREPEND == setCommand.getType()) {
prependCommandType(setCommand, mapName, key, ttl);
}
if (setCommand.shouldReply()) {
textCommandService.sendResponse(setCommand);
}
}
use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.
the class TouchCommandProcessor method handle.
@Override
public void handle(TouchCommand touchCommand) {
String key;
try {
key = URLDecoder.decode(touchCommand.getKey(), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new HazelcastException(e);
}
String mapName = DEFAULT_MAP_NAME;
int index = key.indexOf(':');
if (index != -1) {
mapName = MAP_NAME_PRECEDER + key.substring(0, index);
key = key.substring(index + 1);
}
int ttl = textCommandService.getAdjustedTTLSeconds(touchCommand.getExpiration());
try {
textCommandService.lock(mapName, key);
} catch (Exception e) {
touchCommand.setResponse(NOT_STORED);
if (touchCommand.shouldReply()) {
textCommandService.sendResponse(touchCommand);
}
return;
}
final Object value = textCommandService.get(mapName, key);
textCommandService.incrementTouchCount();
if (value != null) {
textCommandService.put(mapName, key, value, ttl);
touchCommand.setResponse(TOUCHED);
} else {
touchCommand.setResponse(NOT_STORED);
}
textCommandService.unlock(mapName, key);
if (touchCommand.shouldReply()) {
textCommandService.sendResponse(touchCommand);
}
}
Aggregations