use of com.ms.silverking.cloud.dht.VersionConstraint in project SilverKing by Morgan-Stanley.
the class AbstractSegment method retrieve.
@SuppressWarnings("unused")
private ByteBuffer retrieve(DHTKey key, InternalRetrievalOptions options, int offset) {
// code. Need to determine if that code is required.
if (debugRetrieve || Log.levelMet(Level.FINE)) {
Log.warning("segment number: " + getSegmentNumber());
Log.warning("key offset: ", key + " " + offset);
}
if (offset == noSuchKey) {
if (debugRetrieve) {
Log.warning("noSuchKey");
}
return null;
} else {
int storedLength;
ByteBuffer buffer;
boolean doubleCheckVersion;
ByteBuffer returnBuffer;
if (offset < 0) {
OffsetList offsetList;
ValidityVerifier validityVerifier;
doubleCheckVersion = false;
// System.out.printf("offset %d -offset %d\n", offset, -offset);
if (debugRetrieve) {
Log.warning("Looking in offset list: " + -offset);
}
offsetList = offsetListStore.getOffsetList(-offset);
if (debugRetrieve) {
Log.warning("offsetList: ", offsetList);
offsetList.displayForDebug();
}
if (options.getVerifyStorageState()) {
validityVerifier = new ValidityVerifier(dataBuf, options.getCPSSToVerify());
} else {
validityVerifier = null;
}
offset = offsetList.getOffset(options.getVersionConstraint(), validityVerifier);
if (offset < 0) {
// FUTURE - think about this
offset = noSuchKey;
if (debugRetrieve) {
Log.warning("Couldn't find key in offset list. options: " + options.getVersionConstraint());
}
}
if (debugRetrieve || Log.levelMet(Level.FINE)) {
Log.warning("offset list offset: ", key + " " + offset);
}
} else {
doubleCheckVersion = true;
}
if (offset < 0) {
if (debugRetrieve) {
Log.warning("offset < 0");
}
return null;
} else {
offset += DHTKey.BYTES_PER_KEY;
switch(options.getRetrievalType()) {
case VALUE:
case VALUE_AND_META_DATA:
// FUTURE - consider creating a new buffer type to allow creation in one operation
// FUTURE - think about this
storedLength = MetaDataUtil.getStoredLength(dataBuf, offset);
// System.out.println("storedLength: "+ MetaDataUtil.getStoredLength(data, offset));
// buffer = ByteBuffer.wrap(data, offset, storedLength);
buffer = dataBuf.asReadOnlyBuffer();
buffer.position(offset);
buffer.limit(offset + storedLength);
break;
// fall through
case EXISTENCE:
case META_DATA:
// buffer = ByteBuffer.wrap(data, offset, MetaDataUtil.getMetaDataLength(data, offset));
buffer = dataBuf.asReadOnlyBuffer();
buffer.position(offset);
buffer.limit(offset + MetaDataUtil.getMetaDataLength(dataBuf, offset));
if (MetaDataUtil.isSegmented(buffer.slice())) {
// FUTURE THIS CODE IS COPIED FROM VALUE CASES, ELIM THE DUPLICATE CODE
// FUTURE & verify that segmented metadatautil works
storedLength = MetaDataUtil.getStoredLength(dataBuf, offset);
// System.out.println("storedLength: "+ MetaDataUtil.getStoredLength(data, offset));
// buffer = ByteBuffer.wrap(data, offset, storedLength);
buffer = dataBuf.asReadOnlyBuffer();
buffer.position(offset);
buffer.limit(offset + storedLength);
}
break;
default:
throw new RuntimeException();
}
returnBuffer = buffer.slice();
if (doubleCheckVersion) {
VersionConstraint vc;
vc = options.getVersionConstraint();
if (debugRetrieve && returnBuffer != null) {
boolean a;
boolean b;
boolean c;
boolean d;
a = !vc.equals(VersionConstraint.greatest);
b = !vc.matches(MetaDataUtil.getVersion(returnBuffer, 0));
c = vc.getMaxCreationTime() < Long.MAX_VALUE;
d = vc.getMaxCreationTime() < MetaDataUtil.getCreationTime(returnBuffer, 0);
Log.warningf("doubleCheckVersion 1: %s %s", a && b, c && d);
Log.warningf("doubleCheckVersion 2: %s %s %s %s", a, b, c, d);
Log.warningf("MetaDataUtil.getCreationTime(returnBuffer, 0): %d", MetaDataUtil.getCreationTime(returnBuffer, 0));
if (c && d) {
Log.warningf("%d %d", vc.getMaxCreationTime(), MetaDataUtil.getCreationTime(returnBuffer, 0));
Log.warningf("%s %s", new Date(vc.getMaxCreationTime()), new Date(MetaDataUtil.getCreationTime(returnBuffer, 0)));
}
}
// we include some extra checks below to avoid touching the returnBuffer unnecessarily
if ((!vc.equals(VersionConstraint.greatest) && !vc.matches(MetaDataUtil.getVersion(returnBuffer, 0))) || (vc.getMaxCreationTime() < Long.MAX_VALUE && vc.getMaxCreationTime() < MetaDataUtil.getCreationTime(returnBuffer, 0))) {
returnBuffer = null;
}
}
if (debugRetrieve) {
if (returnBuffer != null) {
System.out.println("MetaDataUtil.getCompressedLength: " + MetaDataUtil.getCompressedLength(returnBuffer, 0));
}
Log.warning("returnBuffer: " + returnBuffer);
}
return returnBuffer;
}
}
}
use of com.ms.silverking.cloud.dht.VersionConstraint in project SilverKing by Morgan-Stanley.
the class OffsetListBase method getOffset_linear.
private int getOffset_linear(VersionConstraint vc, ValidityVerifier validityVerifier) {
long bestMatchVersion;
int bestMatchIndex;
bestMatchIndex = Integer.MIN_VALUE;
if (vc.getMode() == VersionConstraint.Mode.GREATEST) {
bestMatchVersion = Long.MIN_VALUE;
} else {
bestMatchVersion = Long.MAX_VALUE;
}
if (debug) {
Log.warning("getOffset: ", vc);
}
// FUTURE - replace linear search
for (int i = 0; i < size(); i++) {
long curVersion;
// StorageTimes are increasing. Exit this loop if we have exceeded the maxStorageTime.
if (debug) {
if (supportsStorageTime) {
Log.warning("vc.getMaxStorageTime()\t" + vc.getMaxCreationTime() + " getStorageTime(i) " + getStorageTime(i));
}
}
if (supportsStorageTime && vc.getMaxCreationTime() != VersionConstraint.noCreationTimeLimit && vc.getMaxCreationTime() < getStorageTime(i)) {
if (debug) {
Log.warning("vc.getMaxStorageTime() < getStorageTime(i)\t", vc.getMaxCreationTime() + " < " + getStorageTime(i));
}
break;
}
if (debug) {
Log.warning(i + "\t" + vc + "\t" + getVersion(i));
displayEntry(i);
}
curVersion = getVersion(i);
assert curVersion >= 0;
if (vc.matches(curVersion)) {
if (vc.getMode() == VersionConstraint.Mode.LEAST) {
if (curVersion <= bestMatchVersion) {
if (validityVerifier == null || validityVerifier.isValid(getOffset(i) + DHTKey.BYTES_PER_KEY)) {
bestMatchIndex = i;
bestMatchVersion = curVersion;
if (debug) {
Log.warning("found new least: ", i + " " + curVersion + " " + bestMatchVersion);
}
}
}
} else {
if (curVersion >= bestMatchVersion) {
if (validityVerifier == null || validityVerifier.isValid(getOffset(i) + DHTKey.BYTES_PER_KEY)) {
bestMatchIndex = i;
bestMatchVersion = curVersion;
if (debug) {
Log.warning("found new greatest: ", i + " " + curVersion + " " + bestMatchVersion);
}
}
}
}
}
}
if (bestMatchIndex < 0) {
if (debug) {
Log.warning("not found");
}
return NO_MATCH_FOUND;
} else {
if (debug) {
Log.warning("found greatest match: ", bestMatchIndex);
}
return getOffset(bestMatchIndex);
}
}
use of com.ms.silverking.cloud.dht.VersionConstraint in project SilverKing by Morgan-Stanley.
the class VersionConstraintComparator method compare.
@Override
public int compare(VersionConstraint c1, VersionConstraint c2) {
int c;
c = c1.getMode().compareTo(c2.getMode());
if (c != 0) {
return c;
} else {
if (c1.getMin() < c2.getMin()) {
return -1;
} else if (c1.getMin() > c2.getMin()) {
return 1;
}
if (c1.getMax() < c2.getMax()) {
return -1;
} else if (c1.getMax() > c2.getMax()) {
return 1;
}
if (c1.getMaxCreationTime() < c2.getMaxCreationTime()) {
return -1;
} else if (c1.getMaxCreationTime() > c2.getMaxCreationTime()) {
return 1;
} else {
return 0;
}
}
}
use of com.ms.silverking.cloud.dht.VersionConstraint in project SilverKing by Morgan-Stanley.
the class ClientTool method _doRetrieve.
private Object _doRetrieve(ClientOptions options, SynchronousNamespacePerspective<String, byte[]> syncNSP, Stopwatch sw, RetrievalType retrievalType, WaitMode waitMode) throws OperationException, IOException {
try {
SynchronousNamespacePerspective<String, byte[]> ns;
RetrievalOptions retrievalOptions;
StoredValue<byte[]> storedValue;
VersionConstraint vc;
if (options.maxVersion > 0) {
// if (options.minVersion >= 0 && options.maxVersion > 0) {
vc = new VersionConstraint(Long.MIN_VALUE, options.maxVersion, VersionConstraint.Mode.GREATEST);
// vc = new VersionConstraint(options.minVersion, options.maxVersion, Mode.NEWEST);
} else {
vc = VersionConstraint.defaultConstraint;
}
retrievalOptions = OptionsHelper.newRetrievalOptions(retrievalType, waitMode, vc);
Log.warning(retrievalOptions);
Log.warning("Getting value");
storedValue = null;
if (options.action == Action.MultiGet) {
Set<String> keys;
Map<String, ? extends StoredValue<byte[]>> storedValues;
keys = ImmutableSet.copyOf(options.key.split(multiKeyDelimiter));
storedValues = null;
sw.reset();
for (int i = 0; i < options.reps; i++) {
// System.out.printf("Calling retrieve %d\n", i);
storedValues = syncNSP.retrieve(keys, retrievalOptions);
// System.out.printf("Done retrieve %d\n", i);
}
sw.stop();
return storedValues;
} else {
sw.reset();
for (int i = 0; i < options.reps; i++) {
// System.out.printf("Calling retrieve %d\n", i);
storedValue = syncNSP.retrieve(options.key, retrievalOptions);
// System.out.printf("Done retrieve %d\n", i);
}
sw.stop();
return storedValue;
}
} catch (RetrievalException re) {
displayRetrievalExceptionDetails(re);
throw re;
}
}
use of com.ms.silverking.cloud.dht.VersionConstraint in project SilverKing by Morgan-Stanley.
the class AsyncPingMultiPong method clientIteration.
public void clientIteration(String pingKey, String pongKeyBase, long version) throws OperationException {
Set<String> pongKeys;
AsyncRetrieval<String, byte[]> asyncRetrieval;
pongKeys = new HashSet<>(numServers);
for (int i = 0; i < numServers; i++) {
for (int j = 0; j < threadsPerServer; j++) {
pongKeys.add(pongKeyBase + "." + i + "." + j);
}
}
if (verbose) {
System.out.println("Put: " + pingKey);
}
if (delayMS != 0) {
if (verbose) {
System.out.print("Sleeping...");
}
ThreadUtil.sleep(delayMS);
if (verbose) {
System.out.println("Awake.");
}
}
try {
AsyncPut<String> put;
put = asyncNSP.put(pingKey, pingKey.getBytes(), defaultPutOptions.version(version));
put.waitForCompletion();
} catch (PutException pe) {
System.out.println("ignoring put exception");
}
if (verbose) {
System.out.println("WaitFor: " + pongKeyBase);
}
asyncRetrieval = asyncNSP.waitFor(pongKeys, asyncNSP.getOptions().getDefaultWaitOptions().versionConstraint(VersionConstraint.exactMatch(version)));
while (asyncRetrieval.getState() == OperationState.INCOMPLETE) {
asyncRetrieval.waitForCompletion(checkIntervalMS, TimeUnit.MILLISECONDS);
if (asyncRetrieval.getState() == OperationState.INCOMPLETE) {
Set<String> incompleteKeys;
incompleteKeys = asyncRetrieval.getIncompleteKeys();
System.out.println("IncompleteKeys: " + incompleteKeys.size());
for (String key : incompleteKeys) {
System.out.println(key);
}
System.out.println();
}
}
if (verbose) {
System.out.println("Received: " + pongKeyBase);
}
}
Aggregations