use of org.apache.storm.utils.WrappedKeyNotFoundException in project storm by apache.
the class BlobStoreUtils method getNimbodesWithLatestSequenceNumberOfBlob.
// Check for latest sequence number of a key inside zookeeper and return nimbodes containing the latest sequence number
public static Set<NimbusInfo> getNimbodesWithLatestSequenceNumberOfBlob(CuratorFramework zkClient, String key) throws Exception {
List<String> stateInfoList;
try {
stateInfoList = zkClient.getChildren().forPath("/blobstore/" + key);
} catch (KeeperException.NoNodeException e) {
// this should be thrown to the caller to indicate that the key is invalid now
throw new WrappedKeyNotFoundException(key);
}
Set<NimbusInfo> nimbusInfoSet = new HashSet<NimbusInfo>();
int latestSeqNumber = getLatestSequenceNumber(stateInfoList);
LOG.debug("getNimbodesWithLatestSequenceNumberOfBlob stateInfo {} version {}", stateInfoList, latestSeqNumber);
// Get the nimbodes with the latest version
for (String state : stateInfoList) {
BlobKeySequenceInfo sequenceInfo = normalizeNimbusHostPortSequenceNumberInfo(state);
if (latestSeqNumber == Integer.parseInt(sequenceInfo.getSequenceNumber())) {
nimbusInfoSet.add(NimbusInfo.parse(sequenceInfo.getNimbusHostPort()));
}
}
LOG.debug("nimbusInfoList {}", nimbusInfoSet);
return nimbusInfoSet;
}
use of org.apache.storm.utils.WrappedKeyNotFoundException in project storm by apache.
the class KeySequenceNumber method getKeySequenceNumber.
public synchronized int getKeySequenceNumber(CuratorFramework zkClient) throws KeyNotFoundException {
TreeSet<Integer> sequenceNumbers = new TreeSet<Integer>();
try {
// Key has not been created yet and it is the first time it is being created
if (zkClient.checkExists().forPath(BlobStoreUtils.getBlobStoreSubtree() + "/" + key) == null) {
zkClient.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE).forPath(BLOBSTORE_MAX_KEY_SEQUENCE_SUBTREE + "/" + key);
zkClient.setData().forPath(BLOBSTORE_MAX_KEY_SEQUENCE_SUBTREE + "/" + key, ByteBuffer.allocate(INT_CAPACITY).putInt(INITIAL_SEQUENCE_NUMBER).array());
return INITIAL_SEQUENCE_NUMBER;
}
// When all nimbodes go down and one or few of them come up
// Unfortunately there might not be an exact way to know which one contains the most updated blob,
// if all go down which is unlikely. Hence there might be a need to update the blob if all go down.
List<String> stateInfoList = zkClient.getChildren().forPath(BlobStoreUtils.getBlobStoreSubtree() + "/" + key);
LOG.debug("stateInfoList-size {} stateInfoList-data {}", stateInfoList.size(), stateInfoList);
if (stateInfoList.isEmpty()) {
return getMaxSequenceNumber(zkClient);
}
LOG.debug("stateInfoSize {}", stateInfoList.size());
// if not assign the highest sequence number.
for (String stateInfo : stateInfoList) {
sequenceNumbers.add(Integer.parseInt(BlobStoreUtils.normalizeNimbusHostPortSequenceNumberInfo(stateInfo).getSequenceNumber()));
}
// Update scenario 2 and 3 explain the code logic written here
// especially when nimbus crashes and comes up after and before update
// respectively.
int currentSeqNumber = getMaxSequenceNumber(zkClient);
if (!checkIfStateContainsCurrentNimbusHost(stateInfoList, nimbusInfo) && !nimbusInfo.isLeader()) {
if (sequenceNumbers.last() < currentSeqNumber) {
return currentSeqNumber;
} else {
return INITIAL_SEQUENCE_NUMBER - 1;
}
}
// after which nimbus-1 comes back up and a read or update is performed.
if (!checkIfStateContainsCurrentNimbusHost(stateInfoList, nimbusInfo) && nimbusInfo.isLeader()) {
incrementMaxSequenceNumber(zkClient, currentSeqNumber);
return currentSeqNumber + 1;
}
// Other scenario it covers is when max-seq-number and nimbus seq number are equal.
if (sequenceNumbers.size() == 1) {
if (sequenceNumbers.first() < currentSeqNumber) {
incrementMaxSequenceNumber(zkClient, currentSeqNumber);
return currentSeqNumber + 1;
} else {
incrementMaxSequenceNumber(zkClient, currentSeqNumber);
return sequenceNumbers.first() + 1;
}
}
// Normal create update sync scenario returns the greatest sequence number in the set
return sequenceNumbers.last();
} catch (KeeperException.NoNodeException e) {
// this should be thrown to the caller to indicate that the key is invalid now
throw new WrappedKeyNotFoundException(key);
} catch (Exception e) {
// in other case, just set this to 0 to trigger re-sync later
LOG.error("Exception {}", e);
return INITIAL_SEQUENCE_NUMBER - 1;
}
}
use of org.apache.storm.utils.WrappedKeyNotFoundException in project storm by apache.
the class HdfsBlobStore method getStoredBlobMeta.
private SettableBlobMeta getStoredBlobMeta(String key) throws KeyNotFoundException {
InputStream in = null;
try {
BlobStoreFile pf = hbs.read(META_PREFIX + key);
try {
in = pf.getInputStream();
} catch (FileNotFoundException fnf) {
throw new WrappedKeyNotFoundException(key);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
in.close();
in = null;
SettableBlobMeta blobMeta = Utils.thriftDeserialize(SettableBlobMeta.class, out.toByteArray());
return blobMeta;
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// Ignored
}
}
}
}
use of org.apache.storm.utils.WrappedKeyNotFoundException in project storm by apache.
the class HdfsBlobStore method extractBlobMeta.
private SettableBlobMeta extractBlobMeta(String key) throws KeyNotFoundException {
if (key == null) {
throw new WrappedKeyNotFoundException("null can not be blob key");
}
SettableBlobMeta meta = cacheMetas.getIfPresent(key);
if (meta == null) {
meta = getStoredBlobMeta(key);
cacheMetas.put(key, meta);
}
return meta;
}
use of org.apache.storm.utils.WrappedKeyNotFoundException in project storm by apache.
the class LocalFsBlobStore method getStoredBlobMeta.
private SettableBlobMeta getStoredBlobMeta(String key) throws KeyNotFoundException {
InputStream in = null;
try {
LocalFsBlobStoreFile pf = fbs.read(META_PREFIX + key);
try {
in = pf.getInputStream();
} catch (FileNotFoundException fnf) {
throw new WrappedKeyNotFoundException(key);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
in.close();
in = null;
return Utils.thriftDeserialize(SettableBlobMeta.class, out.toByteArray());
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// Ignored
}
}
}
}
Aggregations