@Test(expected = GetFailedException.class) public void testGetNoData() throws Exception { NetworkManager node = NetworkTestUtil.getRandomNode(network); String locationKey = randomString(); String contentKey = randomString(); VersionManager<H2HTestData> versionManager = new VersionManager<H2HTestData>(node.getDataManager(), locationKey, contentKey); versionManager.get(); }
public T get() throws GetFailedException { NavigableMap<Number640, Collection<Number160>> digest = dataManager.getDigestLatest(parameters); if (!contentCache.isEmpty() && digest != null && digest.lastEntry() != null && digest.lastEntry().getKey().versionKey().equals(contentCache.lastKey())) { logger.debug("No need for getting from network. Returning cached version. {}", parameters.toString()); return contentCache.lastEntry().getValue(); } else { int delayCounter = 0; int delayWaitTime = random.nextInt(1000) + 1000; int forkAfterGetCounter = 0; int forkAfterGetWaitTime = random.nextInt(1000) + 1000; while (true) { Cache<T> fetchedVersions = new Cache<T>(); int getCounter = 0; int getWaitTime = random.nextInt(1000) + 1000; while (true) { FutureGet futureGet = dataManager.getLatestUnblocked(parameters); futureGet.awaitUninterruptibly(H2HConstants.AWAIT_NETWORK_OPERATION_MS); digestCache.putAll(buildDigest(futureGet.rawDigest())); fetchedVersions.putAll(buildData(futureGet.rawData())); contentCache.putAll(fetchedVersions); if (futureGet.isFailed() || fetchedVersions.isEmpty()) { if (getCounter > GET_FAILED_LIMIT) { logger.warn("Loading of data failed after {} tries. {}", getCounter, parameters.toString()); throw new GetFailedException("Couldn't load data."); } else { logger.warn("Couldn't get data. Try #{}. Retrying. reason = '{}' {}", getCounter++, futureGet.failedReason(), parameters.toString()); try { Thread.sleep(getWaitTime); getWaitTime = getWaitTime * 2; } catch (InterruptedException ignore) { } } } else { break; } } if (hasVersionDelay(fetchedVersions, digestCache) && delayCounter < DELAY_LIMIT) { logger.warn("Detected a version delay. #{}", delayCounter++); try { Thread.sleep(delayWaitTime); delayWaitTime = delayWaitTime * 2; } catch (InterruptedException ignore) { } continue; } Cache<Set<Number160>> latestVersionKeys = getLatest(digestCache); if (latestVersionKeys.size() > 1 && delayCounter < DELAY_LIMIT) { if (forkAfterGetCounter < FORK_AFTER_GET_LIMIT) { logger.warn("Got a version fork. Waiting. #{}", forkAfterGetCounter++); try { Thread.sleep(forkAfterGetWaitTime); forkAfterGetWaitTime = forkAfterGetWaitTime * 2; } catch (InterruptedException ignore) { } continue; } logger.warn("Got a version fork."); throw new GetFailedException("Got a version fork."); } else { if (delayCounter >= DELAY_LIMIT) { logger.warn("Ignoring delay after {} retries.", delayCounter); } if (contentCache.isEmpty()) { logger.warn("Did not find any version."); throw new GetFailedException("No version found. Got null."); } else { try { return (T) contentCache.lastEntry().getValue(); } catch (Exception e) { logger.error("Cannot get the version.", e); throw new GetFailedException( String.format("Cannot get the version. reason = '%s'", e.getMessage())); } } } } } }
VersionManager extends BaseVersionManager<T> { public T get() throws GetFailedException { NavigableMap<Number640, Collection<Number160>> digest = dataManager.getDigestLatest(parameters); if (!contentCache.isEmpty() && digest != null && digest.lastEntry() != null && digest.lastEntry().getKey().versionKey().equals(contentCache.lastKey())) { logger.debug("No need for getting from network. Returning cached version. {}", parameters.toString()); return contentCache.lastEntry().getValue(); } else { int delayCounter = 0; int delayWaitTime = random.nextInt(1000) + 1000; int forkAfterGetCounter = 0; int forkAfterGetWaitTime = random.nextInt(1000) + 1000; while (true) { Cache<T> fetchedVersions = new Cache<T>(); int getCounter = 0; int getWaitTime = random.nextInt(1000) + 1000; while (true) { FutureGet futureGet = dataManager.getLatestUnblocked(parameters); futureGet.awaitUninterruptibly(H2HConstants.AWAIT_NETWORK_OPERATION_MS); digestCache.putAll(buildDigest(futureGet.rawDigest())); fetchedVersions.putAll(buildData(futureGet.rawData())); contentCache.putAll(fetchedVersions); if (futureGet.isFailed() || fetchedVersions.isEmpty()) { if (getCounter > GET_FAILED_LIMIT) { logger.warn("Loading of data failed after {} tries. {}", getCounter, parameters.toString()); throw new GetFailedException("Couldn't load data."); } else { logger.warn("Couldn't get data. Try #{}. Retrying. reason = '{}' {}", getCounter++, futureGet.failedReason(), parameters.toString()); try { Thread.sleep(getWaitTime); getWaitTime = getWaitTime * 2; } catch (InterruptedException ignore) { } } } else { break; } } if (hasVersionDelay(fetchedVersions, digestCache) && delayCounter < DELAY_LIMIT) { logger.warn("Detected a version delay. #{}", delayCounter++); try { Thread.sleep(delayWaitTime); delayWaitTime = delayWaitTime * 2; } catch (InterruptedException ignore) { } continue; } Cache<Set<Number160>> latestVersionKeys = getLatest(digestCache); if (latestVersionKeys.size() > 1 && delayCounter < DELAY_LIMIT) { if (forkAfterGetCounter < FORK_AFTER_GET_LIMIT) { logger.warn("Got a version fork. Waiting. #{}", forkAfterGetCounter++); try { Thread.sleep(forkAfterGetWaitTime); forkAfterGetWaitTime = forkAfterGetWaitTime * 2; } catch (InterruptedException ignore) { } continue; } logger.warn("Got a version fork."); throw new GetFailedException("Got a version fork."); } else { if (delayCounter >= DELAY_LIMIT) { logger.warn("Ignoring delay after {} retries.", delayCounter); } if (contentCache.isEmpty()) { logger.warn("Did not find any version."); throw new GetFailedException("No version found. Got null."); } else { try { return (T) contentCache.lastEntry().getValue(); } catch (Exception e) { logger.error("Cannot get the version.", e); throw new GetFailedException( String.format("Cannot get the version. reason = '%s'", e.getMessage())); } } } } } } }
VersionManager extends BaseVersionManager<T> { public T get() throws GetFailedException { NavigableMap<Number640, Collection<Number160>> digest = dataManager.getDigestLatest(parameters); if (!contentCache.isEmpty() && digest != null && digest.lastEntry() != null && digest.lastEntry().getKey().versionKey().equals(contentCache.lastKey())) { logger.debug("No need for getting from network. Returning cached version. {}", parameters.toString()); return contentCache.lastEntry().getValue(); } else { int delayCounter = 0; int delayWaitTime = random.nextInt(1000) + 1000; int forkAfterGetCounter = 0; int forkAfterGetWaitTime = random.nextInt(1000) + 1000; while (true) { Cache<T> fetchedVersions = new Cache<T>(); int getCounter = 0; int getWaitTime = random.nextInt(1000) + 1000; while (true) { FutureGet futureGet = dataManager.getLatestUnblocked(parameters); futureGet.awaitUninterruptibly(H2HConstants.AWAIT_NETWORK_OPERATION_MS); digestCache.putAll(buildDigest(futureGet.rawDigest())); fetchedVersions.putAll(buildData(futureGet.rawData())); contentCache.putAll(fetchedVersions); if (futureGet.isFailed() || fetchedVersions.isEmpty()) { if (getCounter > GET_FAILED_LIMIT) { logger.warn("Loading of data failed after {} tries. {}", getCounter, parameters.toString()); throw new GetFailedException("Couldn't load data."); } else { logger.warn("Couldn't get data. Try #{}. Retrying. reason = '{}' {}", getCounter++, futureGet.failedReason(), parameters.toString()); try { Thread.sleep(getWaitTime); getWaitTime = getWaitTime * 2; } catch (InterruptedException ignore) { } } } else { break; } } if (hasVersionDelay(fetchedVersions, digestCache) && delayCounter < DELAY_LIMIT) { logger.warn("Detected a version delay. #{}", delayCounter++); try { Thread.sleep(delayWaitTime); delayWaitTime = delayWaitTime * 2; } catch (InterruptedException ignore) { } continue; } Cache<Set<Number160>> latestVersionKeys = getLatest(digestCache); if (latestVersionKeys.size() > 1 && delayCounter < DELAY_LIMIT) { if (forkAfterGetCounter < FORK_AFTER_GET_LIMIT) { logger.warn("Got a version fork. Waiting. #{}", forkAfterGetCounter++); try { Thread.sleep(forkAfterGetWaitTime); forkAfterGetWaitTime = forkAfterGetWaitTime * 2; } catch (InterruptedException ignore) { } continue; } logger.warn("Got a version fork."); throw new GetFailedException("Got a version fork."); } else { if (delayCounter >= DELAY_LIMIT) { logger.warn("Ignoring delay after {} retries.", delayCounter); } if (contentCache.isEmpty()) { logger.warn("Did not find any version."); throw new GetFailedException("No version found. Got null."); } else { try { return (T) contentCache.lastEntry().getValue(); } catch (Exception e) { logger.error("Cannot get the version.", e); throw new GetFailedException( String.format("Cannot get the version. reason = '%s'", e.getMessage())); } } } } } } VersionManager(DataManager dataManager, String locationKey, String contentKey); }
VersionManager extends BaseVersionManager<T> { public T get() throws GetFailedException { NavigableMap<Number640, Collection<Number160>> digest = dataManager.getDigestLatest(parameters); if (!contentCache.isEmpty() && digest != null && digest.lastEntry() != null && digest.lastEntry().getKey().versionKey().equals(contentCache.lastKey())) { logger.debug("No need for getting from network. Returning cached version. {}", parameters.toString()); return contentCache.lastEntry().getValue(); } else { int delayCounter = 0; int delayWaitTime = random.nextInt(1000) + 1000; int forkAfterGetCounter = 0; int forkAfterGetWaitTime = random.nextInt(1000) + 1000; while (true) { Cache<T> fetchedVersions = new Cache<T>(); int getCounter = 0; int getWaitTime = random.nextInt(1000) + 1000; while (true) { FutureGet futureGet = dataManager.getLatestUnblocked(parameters); futureGet.awaitUninterruptibly(H2HConstants.AWAIT_NETWORK_OPERATION_MS); digestCache.putAll(buildDigest(futureGet.rawDigest())); fetchedVersions.putAll(buildData(futureGet.rawData())); contentCache.putAll(fetchedVersions); if (futureGet.isFailed() || fetchedVersions.isEmpty()) { if (getCounter > GET_FAILED_LIMIT) { logger.warn("Loading of data failed after {} tries. {}", getCounter, parameters.toString()); throw new GetFailedException("Couldn't load data."); } else { logger.warn("Couldn't get data. Try #{}. Retrying. reason = '{}' {}", getCounter++, futureGet.failedReason(), parameters.toString()); try { Thread.sleep(getWaitTime); getWaitTime = getWaitTime * 2; } catch (InterruptedException ignore) { } } } else { break; } } if (hasVersionDelay(fetchedVersions, digestCache) && delayCounter < DELAY_LIMIT) { logger.warn("Detected a version delay. #{}", delayCounter++); try { Thread.sleep(delayWaitTime); delayWaitTime = delayWaitTime * 2; } catch (InterruptedException ignore) { } continue; } Cache<Set<Number160>> latestVersionKeys = getLatest(digestCache); if (latestVersionKeys.size() > 1 && delayCounter < DELAY_LIMIT) { if (forkAfterGetCounter < FORK_AFTER_GET_LIMIT) { logger.warn("Got a version fork. Waiting. #{}", forkAfterGetCounter++); try { Thread.sleep(forkAfterGetWaitTime); forkAfterGetWaitTime = forkAfterGetWaitTime * 2; } catch (InterruptedException ignore) { } continue; } logger.warn("Got a version fork."); throw new GetFailedException("Got a version fork."); } else { if (delayCounter >= DELAY_LIMIT) { logger.warn("Ignoring delay after {} retries.", delayCounter); } if (contentCache.isEmpty()) { logger.warn("Did not find any version."); throw new GetFailedException("No version found. Got null."); } else { try { return (T) contentCache.lastEntry().getValue(); } catch (Exception e) { logger.error("Cannot get the version.", e); throw new GetFailedException( String.format("Cannot get the version. reason = '%s'", e.getMessage())); } } } } } } VersionManager(DataManager dataManager, String locationKey, String contentKey); T get(); void put(T networkContent, KeyPair protectionKeys); }
VersionManager extends BaseVersionManager<T> { public T get() throws GetFailedException { NavigableMap<Number640, Collection<Number160>> digest = dataManager.getDigestLatest(parameters); if (!contentCache.isEmpty() && digest != null && digest.lastEntry() != null && digest.lastEntry().getKey().versionKey().equals(contentCache.lastKey())) { logger.debug("No need for getting from network. Returning cached version. {}", parameters.toString()); return contentCache.lastEntry().getValue(); } else { int delayCounter = 0; int delayWaitTime = random.nextInt(1000) + 1000; int forkAfterGetCounter = 0; int forkAfterGetWaitTime = random.nextInt(1000) + 1000; while (true) { Cache<T> fetchedVersions = new Cache<T>(); int getCounter = 0; int getWaitTime = random.nextInt(1000) + 1000; while (true) { FutureGet futureGet = dataManager.getLatestUnblocked(parameters); futureGet.awaitUninterruptibly(H2HConstants.AWAIT_NETWORK_OPERATION_MS); digestCache.putAll(buildDigest(futureGet.rawDigest())); fetchedVersions.putAll(buildData(futureGet.rawData())); contentCache.putAll(fetchedVersions); if (futureGet.isFailed() || fetchedVersions.isEmpty()) { if (getCounter > GET_FAILED_LIMIT) { logger.warn("Loading of data failed after {} tries. {}", getCounter, parameters.toString()); throw new GetFailedException("Couldn't load data."); } else { logger.warn("Couldn't get data. Try #{}. Retrying. reason = '{}' {}", getCounter++, futureGet.failedReason(), parameters.toString()); try { Thread.sleep(getWaitTime); getWaitTime = getWaitTime * 2; } catch (InterruptedException ignore) { } } } else { break; } } if (hasVersionDelay(fetchedVersions, digestCache) && delayCounter < DELAY_LIMIT) { logger.warn("Detected a version delay. #{}", delayCounter++); try { Thread.sleep(delayWaitTime); delayWaitTime = delayWaitTime * 2; } catch (InterruptedException ignore) { } continue; } Cache<Set<Number160>> latestVersionKeys = getLatest(digestCache); if (latestVersionKeys.size() > 1 && delayCounter < DELAY_LIMIT) { if (forkAfterGetCounter < FORK_AFTER_GET_LIMIT) { logger.warn("Got a version fork. Waiting. #{}", forkAfterGetCounter++); try { Thread.sleep(forkAfterGetWaitTime); forkAfterGetWaitTime = forkAfterGetWaitTime * 2; } catch (InterruptedException ignore) { } continue; } logger.warn("Got a version fork."); throw new GetFailedException("Got a version fork."); } else { if (delayCounter >= DELAY_LIMIT) { logger.warn("Ignoring delay after {} retries.", delayCounter); } if (contentCache.isEmpty()) { logger.warn("Did not find any version."); throw new GetFailedException("No version found. Got null."); } else { try { return (T) contentCache.lastEntry().getValue(); } catch (Exception e) { logger.error("Cannot get the version.", e); throw new GetFailedException( String.format("Cannot get the version. reason = '%s'", e.getMessage())); } } } } } } VersionManager(DataManager dataManager, String locationKey, String contentKey); T get(); void put(T networkContent, KeyPair protectionKeys);  }