@Override public void deregister(final String taskId) { checkNotNull("taskId", taskId); final Path path = buildTracePath(taskId); registryHelper.deleteBatch(path); super.deregister(taskId); }
private void initializeRegistryRoot() { try { registryRoot = registryHelper.ensureDirectory(RUNTIME_REGISTRY_PATH); } catch (Exception e) { //uncommon error logger.error("An error was produced during " + VFSRuntimeRegistry.class.getName() + " directories initialization.", e); } }
private Path buildTracePath(final String pipelineExecutionTraceId) { return registryRoot.resolve(registryHelper.md5Hex(pipelineExecutionTraceId) + TRACE_SUFFIX); } }
@Test public void testReadEntries() throws Exception { prepareReadEntries(); List<Object> result = registryHelper.readEntries(rootPath, filter); assertEquals(expectedObjects, result); for (Path path : entryPaths) { verify(registryHelper, times(1)).readEntry(path); } }
@Override public void registerProvider(final Provider provider) { checkNotNull("provider", provider); final Path path = buildProviderPath(provider.getId()); try { registryHelper.storeEntry(path, provider); } catch (Exception e) { //uncommon error logger.error("Unexpected error was produced during provider marshalling/storing, provider: " + provider, e); throw new RuntimeException("Unexpected error was produced during provider marshalling/storing, provider: " + provider, e); } super.registerProvider(provider); }
@Test public void testInit() throws Exception { providers = new ArrayList<>(); providers.addAll(mockProviderListSPI(providerType, ELEMENTS_COUNT)); runtimes = new ArrayList<>(); runtimes.addAll(mockRuntimeList(providerId, "", ELEMENTS_COUNT)); when(registryHelper.readEntries(registryRoot, VFSRegistryHelper.BySuffixFilter.newFilter(PROVIDER_SUFFIX))).thenReturn(providers); when(registryHelper.readEntries(registryRoot, VFSRegistryHelper.BySuffixFilter.newFilter(RUNTIME_SUFFIX))).thenReturn(runtimes); ((VFSRuntimeRegistry) runtimeRegistry).init(); verify(registryHelper, times(2)).ensureDirectory(RUNTIME_REGISTRY_PATH); verify(registryHelper, times(2)).readEntries(registryRoot, VFSRegistryHelper.BySuffixFilter.newFilter(PROVIDER_SUFFIX)); verify(registryHelper, times(2)).readEntries(registryRoot, VFSRegistryHelper.BySuffixFilter.newFilter(RUNTIME_SUFFIX)); for (Object provider : providers) { verifyProviderIsRegistered((Provider) provider); } for (Object runtime : runtimes) { verifyRuntimeIsRegistered((Runtime) runtime); } }
@Test public void testStoreEntryWhenMarshallerExists() throws Exception { when(marshallerRegistry.get(value.getClass())).thenReturn(marshaller); when(marshaller.marshal(value)).thenReturn(MARSHALLED_VALUE); VFSRegistryEntry expectedEntry = new VFSRegistryEntry(value.getClass().getName(), MARSHALLED_VALUE); when(entryMarshaller.marshal(expectedEntry)).thenReturn(MARSHALLED_ENTRY); registryHelper.storeEntry(path, value); verify(marshallerRegistry, times(1)).get(value.getClass()); verify(registryHelper, times(1)).writeBatch(path, MARSHALLED_ENTRY); }
/** * Reads a list of entries from a path by filtering the files by a given filter. * @param rootPath a path for looking of the VFSRegistryEntry files. * @param filter a filter for selecting the files. * @return a list with the unmarshalled objects backed by the filtered files. */ public List<Object> readEntries(final Path rootPath, final DirectoryStream.Filter<Path> filter) throws Exception { final List<Object> entries = new ArrayList<>(); for (Path path : ioService.newDirectoryStream(rootPath, filter)) { try { entries.add(readEntry(path)); } catch (Exception e) { logger.error("An error was produced while processing entry for path: " + path, e); throw e; } } return entries; }
private void initializeRegistry() { try { final List<Object> traces = registryHelper.readEntries(registryRoot, newFilter(TRACE_SUFFIX)); traces.forEach(trace -> super.register((PipelineExecutorTrace) trace)); } catch (Exception e) { logger.error("An error was produced during " + VFSPipelineExecutorRegistry.class.getName() + " initialization.", e); } }
@SuppressWarnings("unchecked") @Before public void setUp() { when(fileSystem.getPath(PROVISIONING_BRANCH, PROVISIONING_PATH)).thenReturn(provisioningPath); when(marshallerRegistry.get(VFSRegistryEntry.class)).thenReturn(entryMarshaller); registryHelper = spy(new VFSRegistryHelper(marshallerRegistry, ioService, fileSystem)); registryHelper.init(); verify(fileSystem, times(1)).getPath(PROVISIONING_BRANCH, PROVISIONING_PATH); }
/** * Stores an Object marshalled value as a VFSRegistryEntry in the target path. * @param path a path for storing the generated VFSRegistryEntry. * @param value an object value to marshall and store. * @throws Exception exceptions might be thrown in cases of filesystem or marshalling errors. */ public void storeEntry(final Path path, final Object value) throws Exception { final Marshaller marshaller = marshallerRegistry.get(value.getClass()); if (marshaller == null) { throw new Exception("No marshaller was found for class: " + value.getClass()); } @SuppressWarnings("unchecked") final String marshalledValue = marshaller.marshal(value); final VFSRegistryEntry entry = new VFSRegistryEntry(value.getClass().getName(), marshalledValue); final String content = entryMarshaller.marshal(entry); writeBatch(path, content); }
@Override public void register(final PipelineExecutorTrace trace) { checkNotNull("trace", trace); final Path path = buildTracePath(trace.getTaskId()); try { registryHelper.storeEntry(path, trace); } catch (Exception e) { //uncommon error logger.error("Unexpected error was produced during trace marshalling/storing, trace: " + trace, e); throw new RuntimeException("Unexpected error was produced during trace marshalling/storing, trace: " + trace, e); } super.register(trace); }
@Test public void testInit() throws Exception { providers = new ArrayList<>(); providers.addAll(mockProviderListSPI(providerType, ELEMENTS_COUNT)); runtimes = new ArrayList<>(); runtimes.addAll(mockRuntimeList(providerId, "", ELEMENTS_COUNT)); when(registryHelper.readEntries(registryRoot, VFSRegistryHelper.BySuffixFilter.newFilter(PROVIDER_SUFFIX))).thenReturn(providers); when(registryHelper.readEntries(registryRoot, VFSRegistryHelper.BySuffixFilter.newFilter(RUNTIME_SUFFIX))).thenReturn(runtimes); ((VFSRuntimeRegistry) runtimeRegistry).init(); verify(registryHelper, times(2)).ensureDirectory(RUNTIME_REGISTRY_PATH); verify(registryHelper, times(2)).readEntries(registryRoot, VFSRegistryHelper.BySuffixFilter.newFilter(PROVIDER_SUFFIX)); verify(registryHelper, times(2)).readEntries(registryRoot, VFSRegistryHelper.BySuffixFilter.newFilter(RUNTIME_SUFFIX)); for (Object provider : providers) { verifyProviderIsRegistered((Provider) provider); } for (Object runtime : runtimes) { verifyRuntimeIsRegistered((Runtime) runtime); } }
@Test public void testReadEntries() throws Exception { prepareReadEntries(); List<Object> result = registryHelper.readEntries(rootPath, filter); assertEquals(expectedObjects, result); for (Path path : entryPaths) { verify(registryHelper, times(1)).readEntry(path); } }
@Test public void testStoreEntryWhenMarshallerExists() throws Exception { when(marshallerRegistry.get(value.getClass())).thenReturn(marshaller); when(marshaller.marshal(value)).thenReturn(MARSHALLED_VALUE); VFSRegistryEntry expectedEntry = new VFSRegistryEntry(value.getClass().getName(), MARSHALLED_VALUE); when(entryMarshaller.marshal(expectedEntry)).thenReturn(MARSHALLED_ENTRY); registryHelper.storeEntry(path, value); verify(marshallerRegistry, times(1)).get(value.getClass()); verify(registryHelper, times(1)).writeBatch(path, MARSHALLED_ENTRY); }
@Test public void testReadEntry() throws Exception { when(ioService.readAllString(path)).thenReturn(MARSHALLED_ENTRY); when(entryMarshaller.unmarshal(MARSHALLED_ENTRY)).thenReturn(entry); when(entry.getContentType()).thenReturn(Object.class.getName()); when(entry.getContent()).thenReturn(MARSHALLED_VALUE); when(marshallerRegistry.get(any(Class.class))).thenReturn(marshaller); Object unmarshalledValue = mock(Object.class); when(marshaller.unmarshal(MARSHALLED_VALUE)).thenReturn(unmarshalledValue); Object result = registryHelper.readEntry(path); assertEquals(unmarshalledValue, result); }
private void initializeRegistry() { try { final List<Object> providers = registryHelper.readEntries(registryRoot, newFilter(PROVIDER_SUFFIX)); providers.forEach(provider -> super.registerProvider((Provider) provider)); final List<Object> runtimes = registryHelper.readEntries(registryRoot, newFilter(RUNTIME_SUFFIX)); runtimes.forEach(runtime -> super.registerRuntime((Runtime) runtime)); } catch (Exception e) { logger.error("An error was produced during " + VFSRuntimeRegistry.class.getName() + " initialization.", e); } }
@SuppressWarnings("unchecked") @Before public void setUp() { when(fileSystem.getPath(PROVISIONING_BRANCH, PROVISIONING_PATH)).thenReturn(provisioningPath); when(marshallerRegistry.get(VFSRegistryEntry.class)).thenReturn(entryMarshaller); registryHelper = spy(new VFSRegistryHelper(marshallerRegistry, ioService, fileSystem)); registryHelper.init(); verify(fileSystem, times(1)).getPath(PROVISIONING_BRANCH, PROVISIONING_PATH); }
@Test public void testWriteBatch() { when(path.getFileSystem()).thenReturn(fileSystem); registryHelper.writeBatch(path, MARSHALLED_VALUE); verify(ioService, times(1)).startBatch(fileSystem); verify(ioService, times(1)).write(path, MARSHALLED_VALUE); verify(ioService, times(1)).endBatch(); }
@Override public void registerRuntime(final Runtime runtime) { checkNotNull("runtime", runtime); final Path path = buildRuntimePath(runtime.getId()); try { registryHelper.storeEntry(path, runtime); } catch (Exception e) { //uncommon error logger.error("Unexpected error was produced during runtime marshalling/storing, runtime: " + runtime, e); throw new RuntimeException("Unexpected error was produced during runtime marshalling/storing, runtime: " + runtime, e); } super.registerRuntime(runtime); }