public void leave(ProgressStatus progressStatus) throws StorageException { try { Key[] keys = overlayNode.leave(); Set<String> resourcesNames = resourceManager.getAllKeys(); log.info("Leaving..."); log.debug("Number of files to transfer: [" + resourcesNames.size() + "]"); if (!keys[0].equals(overlayNode.getKey())) { for (String name : resourcesNames) { Resource resource = resourceManager.find(name); put(resource, keys[0], false, progressStatus); resource.close(); } } resourceManager.deleteAll(); communicationManager.removeObserver(overlayNode.getKey().getValue()); } catch (OverlayException | IOException e) { log.error("Error while leaving dhash node: '" + overlayNode.getKey().toString() + "'"); } }
@Override public void destroyNode(OverlayNode overlayNode) throws OverlayException { overlayNode.leave(); } }
boolean putSync(Resource resource, ProgressStatus progressStatus) throws StorageException { progressStatus.status("put", 0L, 1L); progressStatus.status("overlay-node-lookup", 0L, 1L); Key key = keyFactory.newKey(resource.getId()); log.debug("Resource to put: [" + resource.getId() + "] Hashing: [" + key.getHashing() + "]"); Key lookupKey = overlayNode.lookUp(key); progressStatus.status("overlay-node-lookup", 1L, 1L); if (lookupKey == null) { log.error("Impossible to do put the resource: " + resource.getId() + " in this moment"); throw new StorageException("Impossible to do put the resource: " + resource.getId() + " in this moment"); } log.debug("Lookup key for " + key.getHashing() + ": [" + lookupKey.getValue() + "]"); boolean success = put(resource, lookupKey, true, progressStatus); progressStatus.status("put", 1L, 1L); return success; }
/** * Relocates the resources of the node. * * @param key The node where the files will be relocated. */ public void relocateAllResources(Key key, ProgressStatus progressStatus) throws StorageException { Set<String> resourcesNames = resourceManager.getAllKeys(); log.info("Relocating Files..."); log.debug("Number of files: [" + resourcesNames.size() + "]"); int filesRelocated = 0; for (String name : resourcesNames) { Resource resource = resourceManager.find(name); Key fileKey = getFileKey(name); if (!fileKey.isBetween(key, overlayNode.getKey())) { boolean relocate = put(resource, key, false, progressStatus); filesRelocated++; } } log.info("Files relocated: [" + filesRelocated + "]"); }
/** * Replicates the specified file in its successors. * * @param resourceId The specified {@link Resource} to replicate. * @throws OverlayException */ public void replicateData(String resourceId, ProgressStatus progressStatus) throws OverlayException, StorageException { Key[] succesorList = overlayNode.getNeighborsList(); for (int i = 0; i < Math.min(replicationFactor, succesorList.length); i++) { Resource resource = resourceManager.find(resourceId); log .debug("Replicate File: [" + resource.getId() + "] Hashing: [" + succesorList[i].getHashing() + "]"); log.debug("Replicate to " + succesorList[i].getHashing()); put(resource, succesorList[i], false, progressStatus); } }
/** * Creates dhash node by name and overlay node. The name is escaped using * <code>EscapeChars.forHTML</code>. Adds observer to the communication * * @param name Node name * @param overlayNode Overlay node * @return DHash node */ private DHashNode createNode(String name, OverlayNode overlayNode) { DHashNode dhashNode; MessageProcessorGateway dHashEnviroment; ResourceManager resourceManager = resourceManagerFactory.of(name); dhashNode = getDHashNode(name, overlayNode, resourceManager); ReAssignObserver reAssignObserver = getReAssignObserver(dhashNode); overlayNode.getObservable().addObserver(reAssignObserver); dHashEnviroment = getMessageProcessor(dhashNode, resourceManager); communicationManager.addMessageProcessor(name, dHashEnviroment); communicationManager.addMessageStreamProcessor(name, getMessageStreamProcessor(dhashNode, resourceManager)); log.debug("DHash Node " + name + " Created"); return dhashNode; }
Key lookupKey = overlayNode.lookUp(keyFactory.newKey(id));