You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by ha...@apache.org on 2015/08/01 17:46:04 UTC
[11/21] incubator-brooklyn git commit: Delete deprecated memento
persisters
Delete deprecated memento persisters
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/358806da
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/358806da
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/358806da
Branch: refs/heads/master
Commit: 358806dab5cc19c8e4a6a2dfb9913bda10f5499a
Parents: b53a79a
Author: Aled Sage <al...@gmail.com>
Authored: Tue Jul 28 22:58:50 2015 +0100
Committer: Aled Sage <al...@gmail.com>
Committed: Sat Aug 1 00:13:32 2015 +0100
----------------------------------------------------------------------
.../AbstractBrooklynMementoPersister.java | 129 ----
.../BrooklynMementoPersisterInMemory.java | 211 -------
.../BrooklynMementoPersisterToFile.java | 147 -----
.../BrooklynMementoPersisterToMultiFile.java | 601 -------------------
.../rebind/persister/MementoFileWriter.java | 267 --------
.../rebind/persister/MementoFileWriterSync.java | 159 -----
...mentPlaneSyncRecordPersisterToMultiFile.java | 280 ---------
...BrooklynMementoPersisterToMultiFileTest.java | 68 ---
.../persister/MementoFileWriterSyncTest.java | 128 ----
.../rebind/persister/MementoFileWriterTest.java | 99 ---
10 files changed, 2089 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/358806da/core/src/main/java/brooklyn/entity/rebind/persister/AbstractBrooklynMementoPersister.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/rebind/persister/AbstractBrooklynMementoPersister.java b/core/src/main/java/brooklyn/entity/rebind/persister/AbstractBrooklynMementoPersister.java
deleted file mode 100644
index d3f85df..0000000
--- a/core/src/main/java/brooklyn/entity/rebind/persister/AbstractBrooklynMementoPersister.java
+++ /dev/null
@@ -1,129 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package brooklyn.entity.rebind.persister;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import brooklyn.entity.rebind.PersistenceExceptionHandler;
-import brooklyn.entity.rebind.RebindExceptionHandler;
-import brooklyn.entity.rebind.dto.BrooklynMementoManifestImpl;
-import brooklyn.entity.rebind.dto.MutableBrooklynMemento;
-import brooklyn.mementos.BrooklynMemento;
-import brooklyn.mementos.BrooklynMementoManifest;
-import brooklyn.mementos.BrooklynMementoPersister;
-import brooklyn.mementos.BrooklynMementoRawData;
-import brooklyn.mementos.CatalogItemMemento;
-import brooklyn.mementos.EnricherMemento;
-import brooklyn.mementos.EntityMemento;
-import brooklyn.mementos.LocationMemento;
-import brooklyn.mementos.PolicyMemento;
-
-/**
- * @deprecated since 0.7.0 for production use {@link BrooklynMementoPersisterToObjectStore} instead
- */
-@Deprecated
-public abstract class AbstractBrooklynMementoPersister implements BrooklynMementoPersister {
-
- private static final Logger log = LoggerFactory.getLogger(AbstractBrooklynMementoPersister.class);
-
- protected volatile MutableBrooklynMemento memento = new MutableBrooklynMemento();
-
- @Override
- public BrooklynMementoRawData loadMementoRawData(RebindExceptionHandler exceptionHandler) {
- return null;
- }
-
- @Override
- public BrooklynMemento loadMemento(BrooklynMementoRawData mementoData, LookupContext lookupContext, RebindExceptionHandler exceptionHandler) {
- // Trusting people not to cast+modify, because the in-memory persister wouldn't be used in production code
- return memento;
- }
-
- @Override
- public BrooklynMemento loadMemento(LookupContext lookupContext, RebindExceptionHandler exceptionHandler) {
- return loadMemento(null, lookupContext, exceptionHandler);
- }
-
- @Override
- public BrooklynMementoManifest loadMementoManifest(RebindExceptionHandler exceptionHandler) {
- return loadMementoManifest(null, exceptionHandler);
- }
-
- @Override
- public BrooklynMementoManifest loadMementoManifest(BrooklynMementoRawData mementoData, RebindExceptionHandler exceptionHandler) {
- BrooklynMementoManifestImpl.Builder builder = BrooklynMementoManifestImpl.builder();
- for (EntityMemento entity : memento.getEntityMementos().values()) {
- builder.entity(entity.getId(), entity.getType(), entity.getParent(), entity.getCatalogItemId());
- }
- for (LocationMemento entity : memento.getLocationMementos().values()) {
- builder.location(entity.getId(), entity.getType());
- }
- for (PolicyMemento entity : memento.getPolicyMementos().values()) {
- builder.policy(entity.getId(), entity.getType());
- }
- for (EnricherMemento entity : memento.getEnricherMementos().values()) {
- builder.enricher(entity.getId(), entity.getType());
- }
- for (CatalogItemMemento entity : memento.getCatalogItemMementos().values()) {
- builder.catalogItem(entity);
- }
- return builder.build();
- }
-
- @Override public void enableWriteAccess() {}
- @Override public void disableWriteAccess(boolean graceful) {}
- @Override public void stop(boolean graceful) {}
-
- @Override
- public void checkpoint(BrooklynMemento newMemento, PersistenceExceptionHandler exceptionHandler) {
- memento.reset(checkNotNull(newMemento, "memento"));
- }
-
- public void checkpoint(BrooklynMementoRawData newMemento, PersistenceExceptionHandler exceptionHandler) {
- throw new IllegalStateException("Not supported; use "+BrooklynMementoPersisterToObjectStore.class);
- }
-
- @Override
- public void delta(Delta delta, PersistenceExceptionHandler exceptionHanlder) {
- memento.removeEntities(delta.removedEntityIds());
- memento.removeLocations(delta.removedLocationIds());
- memento.removePolicies(delta.removedPolicyIds());
- memento.removeEnrichers(delta.removedEnricherIds());
- memento.removeCatalogItems(delta.removedCatalogItemIds());
- memento.updateEntityMementos(delta.entities());
- memento.updateLocationMementos(delta.locations());
- memento.updatePolicyMementos(delta.policies());
- memento.updateEnricherMementos(delta.enrichers());
- memento.updateCatalogItemMementos(delta.catalogItems());
- }
-
- @Override
- public void queueDelta(Delta delta) {
- log.warn("Legacy persister ignoring queued delta: "+delta);
- }
-
- @Override
- public String getBackingStoreDescription() {
- return toString();
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/358806da/core/src/main/java/brooklyn/entity/rebind/persister/BrooklynMementoPersisterInMemory.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/rebind/persister/BrooklynMementoPersisterInMemory.java b/core/src/main/java/brooklyn/entity/rebind/persister/BrooklynMementoPersisterInMemory.java
deleted file mode 100644
index dbb1e52..0000000
--- a/core/src/main/java/brooklyn/entity/rebind/persister/BrooklynMementoPersisterInMemory.java
+++ /dev/null
@@ -1,211 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package brooklyn.entity.rebind.persister;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-
-import java.io.File;
-import java.io.IOException;
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.Method;
-import java.util.List;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-
-import brooklyn.basic.BrooklynObject;
-import brooklyn.catalog.CatalogItem;
-import brooklyn.entity.Entity;
-import brooklyn.entity.Feed;
-import brooklyn.entity.basic.EntityInternal;
-import brooklyn.entity.proxying.EntityProxy;
-import brooklyn.entity.rebind.BrooklynObjectType;
-import brooklyn.entity.rebind.PersistenceExceptionHandler;
-import brooklyn.entity.rebind.PersistenceExceptionHandlerImpl;
-import brooklyn.entity.rebind.RebindExceptionHandler;
-import brooklyn.entity.rebind.RebindExceptionHandlerImpl;
-import brooklyn.entity.rebind.RebindManager;
-import brooklyn.location.Location;
-import brooklyn.management.ManagementContext;
-import brooklyn.mementos.BrooklynMemento;
-import brooklyn.mementos.BrooklynMementoManifest;
-import brooklyn.policy.Enricher;
-import brooklyn.policy.Policy;
-import brooklyn.util.collections.MutableList;
-import brooklyn.util.collections.MutableMap;
-import brooklyn.util.exceptions.Exceptions;
-import brooklyn.util.javalang.JavaClassNames;
-import brooklyn.util.javalang.Reflections;
-import brooklyn.util.os.Os;
-import brooklyn.util.time.Duration;
-
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Optional;
-import com.google.common.base.Throwables;
-
-/**
- * @deprecated since 0.7.0 for production use {@link BrooklynMementoPersisterToObjectStore} instead; class be moved to tests
- * <code>
- * new BrooklynMementoPersisterToObjectStore(new InMemoryObjectStore(), classLoader)
- * </code>
- */
-@Deprecated
-public class BrooklynMementoPersisterInMemory extends AbstractBrooklynMementoPersister {
-
- private final ClassLoader classLoader;
- private final boolean checkPersistable;
-
- public BrooklynMementoPersisterInMemory(ClassLoader classLoader) {
- this(classLoader, true);
- }
-
- public BrooklynMementoPersisterInMemory(ClassLoader classLoader, boolean checkPersistable) {
- this.classLoader = checkNotNull(classLoader, "classLoader");
- this.checkPersistable = checkPersistable;
- }
-
- @VisibleForTesting
- @Override
- public void waitForWritesCompleted(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException {
- }
-
- @VisibleForTesting
- @Override
- public void waitForWritesCompleted(Duration timeout) throws InterruptedException, TimeoutException {
- // TODO Could wait for concurrent checkpoint/delta, but don't need to for tests
- // because first waits for checkpoint/delta to have been called by RebindManagerImpl.
- return;
- }
-
- @Override
- public void checkpoint(BrooklynMemento newMemento, PersistenceExceptionHandler exceptionHandler) {
- super.checkpoint(newMemento, exceptionHandler);
- if (checkPersistable) reserializeMemento();
- }
-
- @Override
- public void delta(Delta delta, PersistenceExceptionHandler exceptionHandler) {
- super.delta(delta, exceptionHandler);
- if (checkPersistable) reserializeMemento();
- }
-
- private void reserializeMemento() {
- // To confirm always serializable
- try {
- File tempDir = Os.newTempDir(JavaClassNames.cleanSimpleClassName(this));
- try {
- // TODO Duplicate code for LookupContext in RebindManager
- BrooklynMementoPersisterToMultiFile persister = new BrooklynMementoPersisterToMultiFile(tempDir , classLoader);
- RebindExceptionHandler rebindExceptionHandler = RebindExceptionHandlerImpl.builder()
- .danglingRefFailureMode(RebindManager.RebindFailureMode.FAIL_AT_END)
- .rebindFailureMode(RebindManager.RebindFailureMode.FAIL_AT_END)
- .build();
- PersistenceExceptionHandler persistenceExceptionHandler = PersistenceExceptionHandlerImpl.builder().build();
- persister.checkpoint(memento, persistenceExceptionHandler);
- final BrooklynMementoManifest manifest = persister.loadMementoManifest(null, rebindExceptionHandler);
- LookupContext dummyLookupContext = new LookupContext() {
- @Override
- public ManagementContext lookupManagementContext() {
- return null;
- }
- @Override public Entity lookupEntity(String id) {
- List<Class<?>> types = MutableList.<Class<?>>builder()
- .add(Entity.class, EntityInternal.class, EntityProxy.class)
- //TODO Doesn't support loading from catalog item osgi bundles
- .add(loadClass(manifest.getEntityIdToManifest().get(id).getType()))
- .build();
- return (Entity) java.lang.reflect.Proxy.newProxyInstance(
- classLoader,
- types.toArray(new Class<?>[types.size()]),
- new InvocationHandler() {
- @Override public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
- return m.invoke(this, args);
- }
- });
- }
- @Override public Location lookupLocation(String id) {
- Class<?> clazz = loadClass(manifest.getLocationIdToType().get(id));
- return (Location) invokeConstructor(clazz, new Object[0], new Object[] {MutableMap.of()});
- }
- @Override public Policy lookupPolicy(String id) {
- Class<?> clazz = loadClass(manifest.getPolicyIdToType().get(id));
- return (Policy) invokeConstructor(clazz, new Object[0], new Object[] {MutableMap.of()});
- }
- @Override public Enricher lookupEnricher(String id) {
- Class<?> clazz = loadClass(manifest.getEnricherIdToType().get(id));
- return (Enricher) invokeConstructor(clazz, new Object[0], new Object[] {MutableMap.of()});
- }
- @Override public Feed lookupFeed(String id) {
- Class<?> clazz = loadClass(manifest.getFeedIdToType().get(id));
- return (Feed) invokeConstructor(clazz, new Object[0], new Object[] {MutableMap.of()});
- }
- @Override public CatalogItem<?, ?> lookupCatalogItem(String id) {
- Class<?> clazz = loadClass(manifest.getCatalogItemMemento(id).getType());
- return (CatalogItem<?,?>) invokeConstructor(clazz, new Object[0]);
- }
-
- @Override
- public BrooklynObject lookup(BrooklynObjectType type, String id) {
- switch (type) {
- case CATALOG_ITEM: return lookupCatalogItem(id);
- case ENRICHER: return lookupEnricher(id);
- case ENTITY: return lookupEntity(id);
- case FEED: return lookupFeed(id);
- case LOCATION: return lookupLocation(id);
- case POLICY: return lookupPolicy(id);
- case UNKNOWN: return null;
- }
- throw new IllegalStateException("Unexpected type "+type+" / id "+id);
- }
- @Override
- public BrooklynObject peek(BrooklynObjectType type, String id) {
- return lookup(type, id);
- }
-
- private Class<?> loadClass(String name) {
- try {
- return classLoader.loadClass(name);
- } catch (ClassNotFoundException e) {
- throw Exceptions.propagate(e);
- }
- }
- private <T> T invokeConstructor(Class<T> clazz, Object[]... possibleArgs) {
- for (Object[] args : possibleArgs) {
- try {
- Optional<T> v = Reflections.invokeConstructorWithArgs(clazz, args, true);
- if (v.isPresent()) {
- return v.get();
- }
- } catch (Exception e) {
- throw Exceptions.propagate(e);
- }
- }
- throw new IllegalStateException("Cannot instantiate instance of type "+clazz+"; expected constructor signature not found");
- }
- };
-
- // Not actually reconstituting, because need to use a real lookupContext to reconstitute all the entities
- persister.loadMemento(null, dummyLookupContext, rebindExceptionHandler);
- } finally {
- Os.deleteRecursively(tempDir);
- }
- } catch (IOException e) {
- throw Throwables.propagate(e);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/358806da/core/src/main/java/brooklyn/entity/rebind/persister/BrooklynMementoPersisterToFile.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/rebind/persister/BrooklynMementoPersisterToFile.java b/core/src/main/java/brooklyn/entity/rebind/persister/BrooklynMementoPersisterToFile.java
deleted file mode 100644
index 68dd6eb..0000000
--- a/core/src/main/java/brooklyn/entity/rebind/persister/BrooklynMementoPersisterToFile.java
+++ /dev/null
@@ -1,147 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package brooklyn.entity.rebind.persister;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import brooklyn.entity.rebind.PersistenceExceptionHandler;
-import brooklyn.entity.rebind.RebindExceptionHandler;
-import brooklyn.mementos.BrooklynMemento;
-import brooklyn.util.exceptions.Exceptions;
-import brooklyn.util.time.Duration;
-import brooklyn.util.time.Time;
-
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Charsets;
-import com.google.common.base.Stopwatch;
-import com.google.common.io.Files;
-
-/**
- * @deprecated since 0.7.0 use BrooklynMementoPersisterToMultiFile instead; the single-file version
- * has not been tested recently or kept up-to-date.
- */
-@Deprecated
-public class BrooklynMementoPersisterToFile extends AbstractBrooklynMementoPersister {
-
- // FIXME This is no longer used (instead we use ToMultiFile).
- // Is this definitely no longer useful? Delete if not, and
- // merge AbstractBrooklynMementoPersister+BrooklynMementoPerisisterInMemory.
-
- private static final Logger LOG = LoggerFactory.getLogger(BrooklynMementoPersisterToFile.class);
-
- private final File file;
- private final MementoSerializer<BrooklynMemento> serializer;
- private final Object mutex = new Object();
-
-
- public BrooklynMementoPersisterToFile(File file, ClassLoader classLoader) {
- this.file = file;
- this.serializer = new XmlMementoSerializer<BrooklynMemento>(classLoader);
- }
-
- @VisibleForTesting
- @Override
- public void waitForWritesCompleted(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException {
- }
-
- @VisibleForTesting
- @Override
- public void waitForWritesCompleted(Duration timeout) throws InterruptedException, TimeoutException {
- // TODO Could wait for concurrent checkpoint/delta, but don't need to for tests
- // because they first wait for checkpoint/delta to have been called by RebindManagerImpl.
- return;
- }
-
- @Override
- public BrooklynMemento loadMemento(LookupContext lookupContext, RebindExceptionHandler exceptionHandler) {
- Stopwatch stopwatch = Stopwatch.createStarted();
-
- String xml = readFile();
- serializer.setLookupContext(lookupContext);
- try {
- BrooklynMemento result = serializer.fromString(xml);
-
- if (LOG.isDebugEnabled()) LOG.debug("Loaded memento; took {}", Time.makeTimeStringRounded(stopwatch));
- return result;
-
- } finally {
- serializer.unsetLookupContext();
- }
- }
-
- private String readFile() {
- try {
- synchronized (mutex) {
- return Files.asCharSource(file, Charsets.UTF_8).read();
- }
- } catch (IOException e) {
- LOG.error("Failed to persist memento", e);
- throw Exceptions.propagate(e);
- }
- }
-
- @Override
- public void checkpoint(BrooklynMemento newMemento, PersistenceExceptionHandler exceptionHandler) {
- Stopwatch stopwatch = Stopwatch.createStarted();
- synchronized (mutex) {
- long timeObtainedMutex = stopwatch.elapsed(TimeUnit.MILLISECONDS);
- super.checkpoint(newMemento, exceptionHandler);
- long timeCheckpointed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
- writeMemento();
- long timeWritten = stopwatch.elapsed(TimeUnit.MILLISECONDS);
-
- if (LOG.isDebugEnabled()) LOG.debug("Checkpointed memento; total={}ms, obtainingMutex={}ms, " +
- "checkpointing={}ms, writing={}ms",
- new Object[] {timeWritten, timeObtainedMutex, (timeCheckpointed-timeObtainedMutex),
- (timeWritten-timeCheckpointed)});
- }
- }
-
- @Override
- public void delta(Delta delta, PersistenceExceptionHandler exceptionHandler) {
- Stopwatch stopwatch = Stopwatch.createStarted();
- synchronized (mutex) {
- long timeObtainedMutex = stopwatch.elapsed(TimeUnit.MILLISECONDS);
- super.delta(delta, exceptionHandler);
- long timeDeltad = stopwatch.elapsed(TimeUnit.MILLISECONDS);
- writeMemento();
- long timeWritten = stopwatch.elapsed(TimeUnit.MILLISECONDS);
-
- if (LOG.isDebugEnabled()) LOG.debug("Checkpointed memento; total={}ms, obtainingMutex={}ms, " +
- "delta'ing={}ms, writing={}",
- new Object[] {timeWritten, timeObtainedMutex, (timeDeltad-timeObtainedMutex),
- (timeWritten-timeDeltad)});
- }
- }
-
- private void writeMemento() {
- assert Thread.holdsLock(mutex);
- try {
- Files.write(serializer.toString(memento), file, Charsets.UTF_8);
- } catch (IOException e) {
- LOG.error("Failed to persist memento", e);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/358806da/core/src/main/java/brooklyn/entity/rebind/persister/BrooklynMementoPersisterToMultiFile.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/rebind/persister/BrooklynMementoPersisterToMultiFile.java b/core/src/main/java/brooklyn/entity/rebind/persister/BrooklynMementoPersisterToMultiFile.java
deleted file mode 100644
index afb3d61..0000000
--- a/core/src/main/java/brooklyn/entity/rebind/persister/BrooklynMementoPersisterToMultiFile.java
+++ /dev/null
@@ -1,601 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package brooklyn.entity.rebind.persister;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-
-import java.io.File;
-import java.io.FileFilter;
-import java.io.IOException;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.Executors;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import brooklyn.entity.rebind.BrooklynObjectType;
-import brooklyn.entity.rebind.PersistenceExceptionHandler;
-import brooklyn.entity.rebind.RebindExceptionHandler;
-import brooklyn.entity.rebind.dto.BrooklynMementoImpl;
-import brooklyn.entity.rebind.dto.BrooklynMementoManifestImpl;
-import brooklyn.mementos.BrooklynMemento;
-import brooklyn.mementos.BrooklynMementoManifest;
-import brooklyn.mementos.BrooklynMementoPersister;
-import brooklyn.mementos.BrooklynMementoRawData;
-import brooklyn.mementos.CatalogItemMemento;
-import brooklyn.mementos.EnricherMemento;
-import brooklyn.mementos.EntityMemento;
-import brooklyn.mementos.LocationMemento;
-import brooklyn.mementos.PolicyMemento;
-import brooklyn.util.exceptions.Exceptions;
-import brooklyn.util.text.Strings;
-import brooklyn.util.time.Duration;
-import brooklyn.util.time.Time;
-import brooklyn.util.xstream.XmlUtil;
-
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Charsets;
-import com.google.common.base.Stopwatch;
-import com.google.common.io.Files;
-import com.google.common.util.concurrent.ListeningExecutorService;
-import com.google.common.util.concurrent.MoreExecutors;
-
-/**
- * @deprecated since 0.7.0 use {@link BrooklynMementoPersisterToObjectStore} instead;
- * it has a multi-file filesystem backend for equivalent functionality, but is pluggable
- * to support other storage backends
- */
-@Deprecated
-public class BrooklynMementoPersisterToMultiFile implements BrooklynMementoPersister {
-
- private static final Logger LOG = LoggerFactory.getLogger(BrooklynMementoPersisterToMultiFile.class);
-
- private static final int SHUTDOWN_TIMEOUT_MS = 10*1000;
-
- private final File dir;
-
- private final File entitiesDir;
- private final File locationsDir;
- private final File policiesDir;
- private final File enrichersDir;
- private final File catalogItemsDir;
-
- private final ConcurrentMap<String, MementoFileWriter<EntityMemento>> entityWriters = new ConcurrentHashMap<String, MementoFileWriter<EntityMemento>>();
- private final ConcurrentMap<String, MementoFileWriter<LocationMemento>> locationWriters = new ConcurrentHashMap<String, MementoFileWriter<LocationMemento>>();
- private final ConcurrentMap<String, MementoFileWriter<PolicyMemento>> policyWriters = new ConcurrentHashMap<String, MementoFileWriter<PolicyMemento>>();
- private final ConcurrentMap<String, MementoFileWriter<EnricherMemento>> enricherWriters = new ConcurrentHashMap<String, MementoFileWriter<EnricherMemento>>();
- private final ConcurrentMap<String, MementoFileWriter<CatalogItemMemento>> catalogItemWriters = new ConcurrentHashMap<String, MementoFileWriter<CatalogItemMemento>>();
-
- private final MementoSerializer<Object> serializer;
-
- private final ListeningExecutorService executor;
-
- private static final int MAX_SERIALIZATION_ATTEMPTS = 5;
-
- private volatile boolean running = true;
-
-
- public BrooklynMementoPersisterToMultiFile(File dir, ClassLoader classLoader) {
- this.dir = checkNotNull(dir, "dir");
- MementoSerializer<Object> rawSerializer = new XmlMementoSerializer<Object>(classLoader);
- this.serializer = new RetryingMementoSerializer<Object>(rawSerializer, MAX_SERIALIZATION_ATTEMPTS);
-
- checkDirIsAccessible(dir);
-
- entitiesDir = new File(dir, "entities");
- entitiesDir.mkdir();
- checkDirIsAccessible(entitiesDir);
-
- locationsDir = new File(dir, "locations");
- locationsDir.mkdir();
- checkDirIsAccessible(locationsDir);
-
- policiesDir = new File(dir, "policies");
- policiesDir.mkdir();
- checkDirIsAccessible(policiesDir);
-
- enrichersDir = new File(dir, "enrichers");
- enrichersDir.mkdir();
- checkDirIsAccessible(enrichersDir);
-
- catalogItemsDir = new File(dir, "catalog");
- catalogItemsDir.mkdir();
- checkDirIsAccessible(catalogItemsDir);
-
- File planeDir = new File(dir, "plane");
- planeDir.mkdir();
- checkDirIsAccessible(planeDir);
-
- this.executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
-
- LOG.info("Memento-persister will use directory {}", dir);
- }
-
- @Override public void enableWriteAccess() {
- // no-op -- means subsequent writes will not be enabled
- }
-
- @Override public void disableWriteAccess(boolean graceful) {
- stop(graceful);
- }
-
- @Override
- public void stop(boolean graceful) {
- running = false;
- if (graceful) {
- executor.shutdown();
- try {
- executor.awaitTermination(SHUTDOWN_TIMEOUT_MS, TimeUnit.MILLISECONDS);
- } catch (InterruptedException e) {
- throw Exceptions.propagate(e);
- }
- } else {
- executor.shutdownNow();
- }
- }
-
- @Override
- public BrooklynMementoRawData loadMementoRawData(RebindExceptionHandler exceptionHandler) {
- return null;
- }
-
- @Override
- public BrooklynMementoManifest loadMementoManifest(RebindExceptionHandler exceptionHandler) throws IOException {
- return loadMementoManifest(null, exceptionHandler);
- }
-
- @Override
- public BrooklynMementoManifest loadMementoManifest(BrooklynMementoRawData mementoData, RebindExceptionHandler exceptionHandler) throws IOException {
- if (!running) {
- throw new IllegalStateException("Persister not running; cannot load memento manifest from "+dir);
- }
-
- Stopwatch stopwatch = Stopwatch.createStarted();
-
- FileFilter fileFilter = new FileFilter() {
- @Override public boolean accept(File file) {
- return !file.getName().endsWith(".tmp");
- }
- };
- File[] entityFiles;
- File[] locationFiles;
- File[] policyFiles;
- File[] enricherFiles;
- File[] catalogItemFiles;
- try {
- entityFiles = entitiesDir.listFiles(fileFilter);
- locationFiles = locationsDir.listFiles(fileFilter);
- policyFiles = policiesDir.listFiles(fileFilter);
- enricherFiles = enrichersDir.listFiles(fileFilter);
- catalogItemFiles = catalogItemsDir.listFiles(fileFilter);
- } catch (Exception e) {
- Exceptions.propagateIfFatal(e);
- exceptionHandler.onLoadMementoFailed(BrooklynObjectType.UNKNOWN, "Failed to list files", e);
- throw new IllegalStateException("Failed to list memento files in "+dir, e);
- }
-
- LOG.info("Loading memento manifest from {}; {} entities, {} locations, {} policies, {} enrichers",
- new Object[] {dir, entityFiles.length, locationFiles.length, policyFiles.length, enricherFiles.length});
-
- BrooklynMementoManifestImpl.Builder builder = BrooklynMementoManifestImpl.builder();
-
- try {
- for (File file : entityFiles) {
- try {
- String contents = readFile(file);
- String id = (String) XmlUtil.xpath(contents, "/entity/id");
- String type = (String) XmlUtil.xpath(contents, "/entity/type");
- String parentId = (String) XmlUtil.xpath(contents, "/entity/parent");
- String catalogItemId = (String) XmlUtil.xpath(contents, "/entity/catalogItemId");
- builder.entity(id, type, Strings.emptyToNull(parentId), Strings.emptyToNull(catalogItemId));
- } catch (Exception e) {
- exceptionHandler.onLoadMementoFailed(BrooklynObjectType.ENTITY, "File "+file, e);
- }
- }
- for (File file : locationFiles) {
- try {
- String contents = readFile(file);
- String id = (String) XmlUtil.xpath(contents, "/location/id");
- String type = (String) XmlUtil.xpath(contents, "/location/type");
- builder.location(id, type);
- } catch (Exception e) {
- exceptionHandler.onLoadMementoFailed(BrooklynObjectType.LOCATION, "File "+file, e);
- }
- }
- for (File file : policyFiles) {
- try {
- String contents = readFile(file);
- String id = (String) XmlUtil.xpath(contents, "/policy/id");
- String type = (String) XmlUtil.xpath(contents, "/policy/type");
- builder.policy(id, type);
- } catch (Exception e) {
- exceptionHandler.onLoadMementoFailed(BrooklynObjectType.POLICY, "File "+file, e);
- }
- }
- for (File file : enricherFiles) {
- try {
- String contents = readFile(file);
- String id = (String) XmlUtil.xpath(contents, "/enricher/id");
- String type = (String) XmlUtil.xpath(contents, "/enricher/type");
- builder.enricher(id, type);
- } catch (Exception e) {
- exceptionHandler.onLoadMementoFailed(BrooklynObjectType.ENRICHER, "File "+file, e);
- }
- }
- for (File file : catalogItemFiles) {
- try {
- String contents = readFile(file);
- String id = (String) XmlUtil.xpath(contents, "/catalogItem/id");
- String type = (String) XmlUtil.xpath(contents, "/catalogItem/type");
- builder.enricher(id, type);
- } catch (Exception e) {
- exceptionHandler.onLoadMementoFailed(BrooklynObjectType.CATALOG_ITEM, "File "+file, e);
- }
- }
-
- if (LOG.isDebugEnabled()) LOG.debug("Loaded memento manifest; took {}", Time.makeTimeStringRounded(stopwatch.elapsed(TimeUnit.MILLISECONDS)));
- return builder.build();
-
- } finally {
- serializer.unsetLookupContext();
- }
- }
-
- @Override
- public BrooklynMemento loadMemento(LookupContext lookupContext, RebindExceptionHandler exceptionHandler) throws IOException {
- return loadMemento(null, lookupContext, exceptionHandler);
- }
-
- @Override
- public BrooklynMemento loadMemento(BrooklynMementoRawData mementoData, LookupContext lookupContext, RebindExceptionHandler exceptionHandler) throws IOException {
- if (!running) {
- throw new IllegalStateException("Persister not running; cannot load memento from "+dir);
- }
-
- Stopwatch stopwatch = Stopwatch.createStarted();
-
- FileFilter fileFilter = new FileFilter() {
- @Override public boolean accept(File file) {
- return !file.getName().endsWith(".tmp");
- }
- };
- File[] entityFiles;
- File[] locationFiles;
- File[] policyFiles;
- File[] enricherFiles;
- File[] catalogItemFiles;
- try {
- entityFiles = entitiesDir.listFiles(fileFilter);
- locationFiles = locationsDir.listFiles(fileFilter);
- policyFiles = policiesDir.listFiles(fileFilter);
- enricherFiles = enrichersDir.listFiles(fileFilter);
- catalogItemFiles = catalogItemsDir.listFiles(fileFilter);
- } catch (Exception e) {
- Exceptions.propagateIfFatal(e);
- exceptionHandler.onLoadMementoFailed(BrooklynObjectType.UNKNOWN, "Failed to list files", e);
- throw new IllegalStateException("Failed to list memento files in "+dir, e);
- }
-
- LOG.info("Loading memento from {}; {} entities, {} locations, {} policies, {} enrichers and {} catalog items",
- new Object[] {dir, entityFiles.length, locationFiles.length, policyFiles.length, enricherFiles.length, catalogItemFiles.length});
-
- BrooklynMementoImpl.Builder builder = BrooklynMementoImpl.builder();
-
- serializer.setLookupContext(lookupContext);
- try {
- for (File file : entityFiles) {
- try {
- EntityMemento memento = (EntityMemento) serializer.fromString(readFile(file));
- if (memento == null) {
- LOG.warn("No entity-memento deserialized from file "+file+"; ignoring and continuing");
- } else {
- builder.entity(memento);
- if (memento.isTopLevelApp()) {
- builder.applicationId(memento.getId());
- }
- }
- } catch (Exception e) {
- exceptionHandler.onLoadMementoFailed(BrooklynObjectType.ENTITY, "File "+file, e);
- }
- }
- for (File file : locationFiles) {
- try {
- LocationMemento memento = (LocationMemento) serializer.fromString(readFile(file));
- if (memento == null) {
- LOG.warn("No location-memento deserialized from file "+file+"; ignoring and continuing");
- } else {
- builder.location(memento);
- }
- } catch (Exception e) {
- exceptionHandler.onLoadMementoFailed(BrooklynObjectType.LOCATION, "File "+file, e);
- }
- }
- for (File file : policyFiles) {
- try {
- PolicyMemento memento = (PolicyMemento) serializer.fromString(readFile(file));
- if (memento == null) {
- LOG.warn("No policy-memento deserialized from file "+file+"; ignoring and continuing");
- } else {
- builder.policy(memento);
- }
- } catch (Exception e) {
- exceptionHandler.onLoadMementoFailed(BrooklynObjectType.POLICY, "File "+file, e);
- }
- }
- for (File file : enricherFiles) {
- EnricherMemento memento = (EnricherMemento) serializer.fromString(readFile(file));
- if (memento == null) {
- LOG.warn("No enricher-memento deserialized from file "+file+"; ignoring and continuing");
- } else {
- builder.enricher(memento);
- }
- }
- for (File file : catalogItemFiles) {
- CatalogItemMemento memento = (CatalogItemMemento) serializer.fromString(readFile(file));
- if (memento == null) {
- LOG.warn("No catalog-item-memento deserialized from file "+file+"; ignoring and continuing");
- } else {
- builder.catalogItem(memento);
- }
- }
-
- if (LOG.isDebugEnabled()) LOG.debug("Loaded memento; took {}", Time.makeTimeStringRounded(stopwatch.elapsed(TimeUnit.MILLISECONDS)));
- return builder.build();
-
- } finally {
- serializer.unsetLookupContext();
- }
- }
-
- @Override
- public void checkpoint(BrooklynMemento newMemento, PersistenceExceptionHandler exceptionHandler) {
- if (!running) {
- if (LOG.isDebugEnabled()) LOG.debug("Ignoring checkpointing entire memento, because not running");
- return;
- }
- if (LOG.isDebugEnabled()) LOG.debug("Checkpointing entire memento");
-
- for (EntityMemento m : newMemento.getEntityMementos().values()) {
- persist(m);
- }
- for (LocationMemento m : newMemento.getLocationMementos().values()) {
- persist(m);
- }
- for (PolicyMemento m : newMemento.getPolicyMementos().values()) {
- persist(m);
- }
- for (EnricherMemento m : newMemento.getEnricherMementos().values()) {
- persist(m);
- }
- for (CatalogItemMemento m : newMemento.getCatalogItemMementos().values()) {
- persist(m);
- }
- LOG.warn("Using legacy persister; feeds will not be persisted");
- }
-
- @Override
- public void checkpoint(BrooklynMementoRawData newMemento, PersistenceExceptionHandler exceptionHandler) {
- throw new IllegalStateException("Not supported; use "+BrooklynMementoPersisterToObjectStore.class);
- }
-
- @Override
- public void delta(Delta delta, PersistenceExceptionHandler exceptionHandler) {
- if (!running) {
- if (LOG.isDebugEnabled()) LOG.debug("Ignoring checkpointed delta of memento, because not running");
- return;
- }
- if (LOG.isTraceEnabled()) LOG.trace("Checkpointed delta of memento; updating {} entities, {} locations, {} policies and {} enrichers; " +
- "removing {} entities, {} locations {} policies and {} enrichers",
- new Object[] {delta.entities(), delta.locations(), delta.policies(), delta.enrichers(),
- delta.removedEntityIds(), delta.removedLocationIds(), delta.removedPolicyIds(), delta.removedEnricherIds()});
-
- for (EntityMemento entity : delta.entities()) {
- persist(entity);
- }
- for (LocationMemento location : delta.locations()) {
- persist(location);
- }
- for (PolicyMemento policy : delta.policies()) {
- persist(policy);
- }
- for (EnricherMemento enricher : delta.enrichers()) {
- persist(enricher);
- }
- for (CatalogItemMemento catalogItem : delta.catalogItems()) {
- persist(catalogItem);
- }
- for (String id : delta.removedEntityIds()) {
- deleteEntity(id);
- }
- for (String id : delta.removedLocationIds()) {
- deleteLocation(id);
- }
- for (String id : delta.removedPolicyIds()) {
- deletePolicy(id);
- }
- for (String id : delta.removedEnricherIds()) {
- deleteEnricher(id);
- }
- for (String id : delta.removedCatalogItemIds()) {
- deleteCatalogItem(id);
- }
- }
-
- @Override
- public void queueDelta(Delta delta) {
- LOG.warn("Legacy persister ignoring queued delta: "+delta);
- }
-
- @VisibleForTesting
- public File getDir() {
- return dir;
- }
-
- @Override
- @VisibleForTesting
- public void waitForWritesCompleted(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException {
- waitForWritesCompleted(Duration.of(timeout, unit));
- }
-
- public void waitForWritesCompleted(Duration timeout) throws InterruptedException, TimeoutException {
- for (MementoFileWriter<?> writer : entityWriters.values()) {
- writer.waitForWriteCompleted(timeout);
- }
- for (MementoFileWriter<?> writer : locationWriters.values()) {
- writer.waitForWriteCompleted(timeout);
- }
- for (MementoFileWriter<?> writer : policyWriters.values()) {
- writer.waitForWriteCompleted(timeout);
- }
- for (MementoFileWriter<?> writer : enricherWriters.values()) {
- writer.waitForWriteCompleted(timeout);
- }
- for (MementoFileWriter<?> writer : catalogItemWriters.values()) {
- writer.waitForWriteCompleted(timeout);
- }
- }
-
- // TODO Promote somewhere sensible; share code with BrooklynLauncher.checkPersistenceDirAccessible
- public static void checkDirIsAccessible(File dir) {
- if (!(dir.exists() && dir.isDirectory() && dir.canRead() && dir.canWrite())) {
- throw new IllegalStateException("Invalid directory "+dir+" because "+
- (!dir.exists() ? "does not exist" :
- (!dir.isDirectory() ? "not a directory" :
- (!dir.canRead() ? "not readable" :
- (!dir.canWrite() ? "not writable" : "unknown reason")))));
- }
- }
-
- private String readFile(File file) throws IOException {
- return Files.asCharSource(file, Charsets.UTF_8).read();
- }
-
- private void persist(EntityMemento entity) {
- MementoFileWriter<EntityMemento> writer = entityWriters.get(entity.getId());
- if (writer == null) {
- entityWriters.putIfAbsent(entity.getId(), new MementoFileWriter<EntityMemento>(getFileFor(entity), executor, serializer));
- writer = entityWriters.get(entity.getId());
- }
- writer.write(entity);
- }
-
- private void persist(LocationMemento location) {
- MementoFileWriter<LocationMemento> writer = locationWriters.get(location.getId());
- if (writer == null) {
- locationWriters.putIfAbsent(location.getId(), new MementoFileWriter<LocationMemento>(getFileFor(location), executor, serializer));
- writer = locationWriters.get(location.getId());
- }
- writer.write(location);
- }
-
- private void persist(PolicyMemento policy) {
- MementoFileWriter<PolicyMemento> writer = policyWriters.get(policy.getId());
- if (writer == null) {
- policyWriters.putIfAbsent(policy.getId(), new MementoFileWriter<PolicyMemento>(getFileFor(policy), executor, serializer));
- writer = policyWriters.get(policy.getId());
- }
- writer.write(policy);
- }
-
- private void persist(EnricherMemento enricher) {
- MementoFileWriter<EnricherMemento> writer = enricherWriters.get(enricher.getId());
- if (writer == null) {
- enricherWriters.putIfAbsent(enricher.getId(), new MementoFileWriter<EnricherMemento>(getFileFor(enricher), executor, serializer));
- writer = enricherWriters.get(enricher.getId());
- }
- writer.write(enricher);
- }
-
- private void persist(CatalogItemMemento catalogItem) {
- MementoFileWriter<CatalogItemMemento> writer = catalogItemWriters.get(catalogItem.getId());
- if (writer == null) {
- catalogItemWriters.putIfAbsent(catalogItem.getId(), new MementoFileWriter<CatalogItemMemento>(getFileFor(catalogItem), executor, serializer));
- writer = catalogItemWriters.get(catalogItem.getId());
- }
- writer.write(catalogItem);
- }
-
- private void deleteEntity(String id) {
- MementoFileWriter<EntityMemento> writer = entityWriters.get(id);
- if (writer != null) {
- writer.delete();
- }
- }
-
- private void deleteLocation(String id) {
- MementoFileWriter<LocationMemento> writer = locationWriters.get(id);
- if (writer != null) {
- writer.delete();
- }
- }
-
- private void deletePolicy(String id) {
- MementoFileWriter<PolicyMemento> writer = policyWriters.get(id);
- if (writer != null) {
- writer.delete();
- }
- }
-
- private void deleteEnricher(String id) {
- MementoFileWriter<EnricherMemento> writer = enricherWriters.get(id);
- if (writer != null) {
- writer.delete();
- }
- }
-
- private void deleteCatalogItem(String id) {
- MementoFileWriter<CatalogItemMemento> writer = catalogItemWriters.get(id);
- if (writer != null) {
- writer.delete();
- }
- }
-
- private File getFileFor(EntityMemento entity) {
- return getFileFor(entitiesDir, entity.getId());
- }
-
- private File getFileFor(LocationMemento location) {
- return getFileFor(locationsDir, location.getId());
- }
-
- private File getFileFor(PolicyMemento policy) {
- return getFileFor(policiesDir, policy.getId());
- }
-
- private File getFileFor(EnricherMemento enricher) {
- return getFileFor(enrichersDir, enricher.getId());
- }
-
- private File getFileFor(CatalogItemMemento catalogItem) {
- return getFileFor(catalogItemsDir, catalogItem.getId());
- }
-
- private File getFileFor(File parent, String id) {
- return new File(parent, Strings.makeValidFilename(id));
- }
-
- @Override
- public String getBackingStoreDescription() {
- return toString();
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/358806da/core/src/main/java/brooklyn/entity/rebind/persister/MementoFileWriter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/rebind/persister/MementoFileWriter.java b/core/src/main/java/brooklyn/entity/rebind/persister/MementoFileWriter.java
deleted file mode 100644
index f6b1a3a..0000000
--- a/core/src/main/java/brooklyn/entity/rebind/persister/MementoFileWriter.java
+++ /dev/null
@@ -1,267 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package brooklyn.entity.rebind.persister;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.concurrent.Callable;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicLong;
-import java.util.concurrent.atomic.AtomicReference;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Charsets;
-import com.google.common.base.Objects;
-import com.google.common.base.Stopwatch;
-import com.google.common.io.Files;
-import com.google.common.util.concurrent.ListenableFuture;
-import com.google.common.util.concurrent.ListeningExecutorService;
-import com.google.common.util.concurrent.MoreExecutors;
-
-import brooklyn.util.exceptions.Exceptions;
-import brooklyn.util.time.Duration;
-import brooklyn.util.time.Time;
-
-/**
- * For asynchronously writing to a file.
- *
- * This class is thread-safe. If a write is in progress, one will be scheduled. If a write is already
- * scheduled, we will just rely on the existing one; otherwise we will write now.
- *
- * @author aled
- * @deprecated since 0.7.0; used only by now-deprecated {@link BrooklynMementoPersisterToMultiFile},
- * impl largely moved to {@link FileBasedStoreObjectAccessor}
- */
-@Deprecated
-public class MementoFileWriter<T> {
-
- private static final Logger LOG = LoggerFactory.getLogger(MementoFileWriter.class);
-
- private final File file;
- private final File tmpFile;
- private final ListeningExecutorService executor;
- private final MementoSerializer<? super T> serializer;
- private final AtomicBoolean executing = new AtomicBoolean();
- private final AtomicReference<T> requireWrite = new AtomicReference<T>();
- private final AtomicBoolean requireDelete = new AtomicBoolean();
- private final AtomicBoolean deleted = new AtomicBoolean();
- private final AtomicLong modCount = new AtomicLong();
-
- /**
- * @param file
- * @param executor A sequential executor (e.g. SingleThreadedExecutor, or equivalent)
- * @param serializer
- */
- public MementoFileWriter(File file, ListeningExecutorService executor, MementoSerializer<? super T> serializer) {
- this.file = file;
- this.executor = executor;
- this.serializer = serializer;
- this.tmpFile = new File(file.getParentFile(), file.getName()+".tmp");
- }
-
- public void write(T val) {
- requireWrite.set(val);
- if (requireDelete.get() || deleted.get()) {
- LOG.warn("Not writing {}, because already deleted", file);
- } else if (executing.compareAndSet(false, true)) {
- if (LOG.isTraceEnabled()) LOG.trace("Submitting write task for {}", file);
- writeAsync();
- } else {
- if (LOG.isTraceEnabled()) LOG.trace("Execution already in-progress for {}; recorded write-requirement; returning", file);
- }
- }
-
- public void delete() {
- if (deleted.get() || requireDelete.get()) {
- if (LOG.isDebugEnabled()) LOG.debug("Duplicate call to delete {}; ignoring", file);
- return;
- }
-
- requireWrite.set(null);
- requireDelete.set(true);
- if (executing.compareAndSet(false, true)) {
- if (LOG.isTraceEnabled()) LOG.trace("Submitting delete task for {}", file);
- deleteAsync();
- } else {
- if (LOG.isTraceEnabled()) LOG.trace("Execution already in-progress for {}; recorded delete-requirement; returning", file);
- }
- }
-
- /**
- * This method must only be used for testing. If required in production, then revisit implementation!
- */
- @VisibleForTesting
- public void waitForWriteCompleted(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException {
- waitForWriteCompleted(Duration.of(timeout, unit));
- }
-
- @VisibleForTesting
- public void waitForWriteCompleted(Duration timeout) throws InterruptedException, TimeoutException {
- // Every time we finish writing, we increment a counter. We note the current val, and then
- // wait until we can guarantee that a complete additional write has been done. Not sufficient
- // to wait for `writeCount > origWriteCount` because we might have read the value when it was
- // almost finished a write.
-
- long timeoutMillis = timeout.toMilliseconds();
- long startTime = System.currentTimeMillis();
- long maxEndtime = (timeoutMillis > 0) ? (startTime + timeoutMillis) : (timeoutMillis < 0) ? startTime : Long.MAX_VALUE;
- long origModCount = modCount.get();
- while (true) {
- if (modCount.get() > (origModCount+1)) {
- return;
- } else if (requireWrite.get() != null) {
- // must continue waiting for mods+1
- } else if (executing.get()) {
- // must wait for either this invocation to complete, or mods+1 (because might have already updated)
- } else {
- return;
- }
-
- if (System.currentTimeMillis() > maxEndtime) {
- throw new TimeoutException("Timeout waiting for pending complete of rebind-periodic-delta, after "+Time.makeTimeStringRounded(timeout));
- }
- Thread.sleep(10);
- }
- }
-
- private void deleteAsync() {
- ListenableFuture<Void> future = executor.submit(new Callable<Void>() {
- @Override public Void call() throws IOException {
- try {
- deleteNow();
- return null;
- } catch (Throwable t) {
- if (executor.isShutdown()) {
- LOG.debug("Error deleting "+file+" (but executor shutdown)", t);
- return null; // just return without throwing; no more work to do
- } else {
- LOG.error("Error deleting "+file, t);
- throw Exceptions.propagate(t);
- }
- }
- }});
- addPostExecListener(future);
- }
-
- private void writeAsync() {
- ListenableFuture<Void> future = executor.submit(new Callable<Void>() {
- @Override public Void call() throws IOException {
- try {
- writeNow();
- return null;
- } catch (Throwable t) {
- if (executor.isShutdown()) {
- LOG.debug("Error writing to "+file+" (but executor shutdown)", t);
- return null; // just return without throwing; no more work to do
- } else {
- LOG.error("Error writing to "+file, t);
- throw Exceptions.propagate(t);
- }
- }
- }});
- addPostExecListener(future);
- }
-
- private void addPostExecListener(ListenableFuture<?> future) {
- future.addListener(
- new Runnable() {
- @Override public void run() {
- if (LOG.isTraceEnabled()) LOG.trace("Write complete for {}", file);
- try {
- executing.set(false);
- if (requireDelete.get()) {
- if (executing.compareAndSet(false, true)) {
- if (LOG.isTraceEnabled()) LOG.trace("Submitting delete-task for {} (in post-exec) due to recorded delete-requirement", file);
- deleteAsync();
- } else {
- if (LOG.isTraceEnabled()) LOG.trace("Delete-requirement for {} (in post-exec) handled by other thread; returning", file);
- }
-
- } else if (requireWrite.get() != null) {
- if (executing.compareAndSet(false, true)) {
- if (LOG.isTraceEnabled()) LOG.trace("Submitting write task for {} (in post-exec) due to recorded write-requirement", file);
- writeAsync();
- } else {
- if (LOG.isTraceEnabled()) LOG.trace("Write-requirement for {} (in post-exec) handled by other thread; returning", file);
- }
- } else {
- if (LOG.isTraceEnabled()) LOG.trace("No pending exec-requirements for {}", file);
- }
- } catch (Throwable t) {
- if (executor.isShutdown()) {
- LOG.debug("Error in post-exec for "+file+" (but executor shutdown)", t);
- return; // just return without throwing; no more work to do
- } else {
- LOG.error("Error in post-exec for "+file, t);
- throw Exceptions.propagate(t);
- }
- }
- }
- },
- MoreExecutors.sameThreadExecutor());
- }
-
- private void writeNow() throws IOException {
- T val = requireWrite.getAndSet(null);
-
- /*
- * Need to guarantee "happens before", with any thread that has written
- * fields of these mementos. In particular, saw failures where SshMachineLocation
- * had null address field. Our hypothesis is that another thread wrote the memento,
- * but that no synchronization subsequently happened so we did not see all the values
- * in that memento from this thread.
- *
- * See PeriodicDeltaChangeListener.persistNow for the corresponding synchronization,
- * that guarantees its thread made the writes visible.
- */
- synchronized (new Object()) {}
-
- Stopwatch stopwatch = Stopwatch.createStarted();
-
- // Write to the temp file, then atomically move it to the permanent file location
- Files.write(serializer.toString(val), tmpFile, Charsets.UTF_8);
- Files.move(tmpFile, file);
-
- modCount.incrementAndGet();
-
- if (LOG.isTraceEnabled()) LOG.trace("Wrote {}, took {}; modified file {} times",
- new Object[] {file, Time.makeTimeStringRounded(stopwatch), modCount});
- }
-
- private void deleteNow() throws IOException {
- if (LOG.isTraceEnabled()) LOG.trace("Deleting {} and {}", file, tmpFile);
- deleted.set(true);
- requireDelete.set(false);
-
- file.delete();
- tmpFile.delete();
-
- modCount.incrementAndGet();
- }
-
- @Override
- public String toString() {
- return Objects.toStringHelper(this).add("file", file).toString();
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/358806da/core/src/main/java/brooklyn/entity/rebind/persister/MementoFileWriterSync.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/rebind/persister/MementoFileWriterSync.java b/core/src/main/java/brooklyn/entity/rebind/persister/MementoFileWriterSync.java
deleted file mode 100644
index ea49111..0000000
--- a/core/src/main/java/brooklyn/entity/rebind/persister/MementoFileWriterSync.java
+++ /dev/null
@@ -1,159 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package brooklyn.entity.rebind.persister;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-import java.util.concurrent.atomic.AtomicLong;
-import java.util.concurrent.locks.ReadWriteLock;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import brooklyn.util.exceptions.Exceptions;
-import brooklyn.util.time.Duration;
-import brooklyn.util.time.Time;
-
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Charsets;
-import com.google.common.base.Objects;
-import com.google.common.base.Stopwatch;
-import com.google.common.io.Files;
-
-/**
- * For synchronously writing to a file - all calls block.
- *
- * This class is thread-safe. If a write/delete/append is in progress, then subsequent calls will
- * block.
- *
- * @author aled
- * @deprecated since 0.7.0 we use {@link PersistenceObjectStore} instances now, and when we need sync behaviour we just wait
- */
-@Deprecated
-public class MementoFileWriterSync<T> {
-
- private static final Logger LOG = LoggerFactory.getLogger(MementoFileWriterSync.class);
-
- private final ReadWriteLock lock = new ReentrantReadWriteLock();
- private final File file;
- private final File tmpFile;
- private final MementoSerializer<? super T> serializer;
- private final AtomicLong modCount = new AtomicLong();
-
- /**
- * @param file
- * @param serializer
- */
- public MementoFileWriterSync(File file, MementoSerializer<? super T> serializer) {
- this(file, serializer, "tmp");
- }
-
- public MementoFileWriterSync(File file, MementoSerializer<? super T> serializer, String tmpFileSuffix) {
- this.file = file;
- this.serializer = serializer;
- this.tmpFile = new File(file.getParentFile(), file.getName()+"."+tmpFileSuffix);
- }
-
- public boolean exists() {
- return file.exists();
- }
-
- public void write(T val) {
- try {
- lock.writeLock().lockInterruptibly();
- } catch (InterruptedException e) {
- throw Exceptions.propagate(e);
- }
- try {
- Stopwatch stopwatch = Stopwatch.createStarted();
-
- // Write to the temp file, then atomically move it to the permanent file location
- Files.write(serializer.toString(val), tmpFile, Charsets.UTF_8);
- Files.move(tmpFile, file);
- modCount.incrementAndGet();
-
- if (LOG.isTraceEnabled()) LOG.trace("Wrote {}, took {}; modified file {} times",
- new Object[] {file, Time.makeTimeStringRounded(stopwatch), modCount});
- } catch (IOException e) {
- throw Exceptions.propagate(e);
- } finally {
- lock.writeLock().unlock();
- }
- }
-
- public void append(T val) {
- try {
- lock.writeLock().lockInterruptibly();
- } catch (InterruptedException e) {
- throw Exceptions.propagate(e);
- }
- try {
- Stopwatch stopwatch = Stopwatch.createStarted();
-
- // Write to the temp file, then atomically move it to the permanent file location
- Files.append(serializer.toString(val), file, Charsets.UTF_8);
- modCount.incrementAndGet();
-
- if (LOG.isTraceEnabled()) LOG.trace("Wrote {}, took {}; modified file {} times",
- new Object[] {file, Time.makeTimeStringRounded(stopwatch), modCount});
- } catch (IOException e) {
- throw Exceptions.propagate(e);
- } finally {
- lock.writeLock().unlock();
- }
- }
-
- public void delete() {
- try {
- lock.writeLock().lockInterruptibly();
- } catch (InterruptedException e) {
- throw Exceptions.propagate(e);
- }
- try {
- Stopwatch stopwatch = Stopwatch.createStarted();
-
- file.delete();
- tmpFile.delete();
- modCount.incrementAndGet();
-
- if (LOG.isTraceEnabled()) LOG.trace("Deleted {}, took {}; modified file {} times",
- new Object[] {file, Time.makeTimeStringRounded(stopwatch), modCount});
- } finally {
- lock.writeLock().unlock();
- }
- }
-
- @VisibleForTesting
- public void waitForWriteCompleted(Duration timeout) throws InterruptedException, TimeoutException {
- boolean locked = lock.writeLock().tryLock(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
- if (locked) {
- lock.writeLock().unlock();
- } else {
- throw new TimeoutException("Timeout waiting for lock on "+file);
- }
- }
-
- @Override
- public String toString() {
- return Objects.toStringHelper(this).add("file", file).toString();
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/358806da/core/src/main/java/brooklyn/management/ha/ManagementPlaneSyncRecordPersisterToMultiFile.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/management/ha/ManagementPlaneSyncRecordPersisterToMultiFile.java b/core/src/main/java/brooklyn/management/ha/ManagementPlaneSyncRecordPersisterToMultiFile.java
deleted file mode 100644
index fcade26..0000000
--- a/core/src/main/java/brooklyn/management/ha/ManagementPlaneSyncRecordPersisterToMultiFile.java
+++ /dev/null
@@ -1,280 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package brooklyn.management.ha;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-
-import java.io.File;
-import java.io.FileFilter;
-import java.io.IOException;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import brooklyn.entity.rebind.persister.BrooklynMementoPersisterToMultiFile;
-import brooklyn.entity.rebind.persister.FileBasedStoreObjectAccessor;
-import brooklyn.entity.rebind.persister.MementoFileWriterSync;
-import brooklyn.entity.rebind.persister.MementoSerializer;
-import brooklyn.entity.rebind.persister.RetryingMementoSerializer;
-import brooklyn.entity.rebind.persister.XmlMementoSerializer;
-import brooklyn.entity.rebind.plane.dto.ManagementPlaneSyncRecordImpl;
-import brooklyn.util.exceptions.Exceptions;
-import brooklyn.util.time.Duration;
-import brooklyn.util.time.Time;
-
-import com.google.common.annotations.Beta;
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Charsets;
-import com.google.common.base.Stopwatch;
-import com.google.common.io.Files;
-
-/**
- * Structure of files is:
- * <ul>
- * <li>{@code plane/} - top-level directory
- * <ul>
- * <li>{@code master} - contains the id of the management-node that is currently master
- * <li>{@code change.log} - log of changes made
- * <li>{@code nodes/} - sub-directory, containing one file per management-node
- * <ul>
- * <li>{@code a9WiuVKp} - file named after the management-node's id, containing the management node's current state
- * <li>{@code E1eDXQF3}
- * </ul>
- * </ul>
- * </ul>
- *
- * All writes are done synchronously.
- *
- * @since 0.7.0
- *
- * @author aled
- * @deprecated since 0.7.0 use {@link ManagementPlaneSyncRecordPersisterToObjectStore} e.g. with {@link FileBasedStoreObjectAccessor}
- */
-@Beta
-@Deprecated
-public class ManagementPlaneSyncRecordPersisterToMultiFile implements ManagementPlaneSyncRecordPersister {
-
- // TODO Multiple node appending to change.log could cause strange interleaving, or perhaps even data loss?
- // But this file is not critical to functionality.
-
- // TODO Should ManagementPlaneSyncRecordPersister.Delta be different so can tell what is a significant event,
- // and thus log it in change.log - currently only subset of significant things being logged.
-
- private static final Logger LOG = LoggerFactory.getLogger(ManagementPlaneSyncRecordPersisterToMultiFile.class);
-
- private static final Duration SHUTDOWN_TIMEOUT = Duration.TEN_SECONDS;
-
- private final String tmpSuffix;
- private final File dir;
- private final File nodesDir;
-
- // TODO Leak if we go through lots of managers; but tiny!
- private final ConcurrentMap<String, MementoFileWriterSync<ManagementNodeSyncRecord>> nodeWriters = new ConcurrentHashMap<String, MementoFileWriterSync<ManagementNodeSyncRecord>>();
-
- private final MementoFileWriterSync<String> masterWriter;
-
- private final MementoFileWriterSync<String> changeLogWriter;
-
- private final MementoSerializer<Object> serializer;
-
- private static final int MAX_SERIALIZATION_ATTEMPTS = 5;
-
- private volatile boolean running = true;
-
- /**
- * @param dir Directory to write management-plane data
- * @param classLoader ClassLoader to use when deserializing data
- * @param id Unique identifier, e.g. used for temp file suffix in case multpile concurrent writers
- */
- public ManagementPlaneSyncRecordPersisterToMultiFile(File dir, ClassLoader classLoader, String id) {
- this.dir = checkNotNull(dir, "dir");
- MementoSerializer<Object> rawSerializer = new XmlMementoSerializer<Object>(checkNotNull(classLoader, "classLoader"));
- this.serializer = new RetryingMementoSerializer<Object>(rawSerializer, MAX_SERIALIZATION_ATTEMPTS);
- this.tmpSuffix = checkNotNull(id, "id")+".tmp"; // important to end in .tmp for loadMemento's file filter
-
- BrooklynMementoPersisterToMultiFile.checkDirIsAccessible(dir);
-
- nodesDir = new File(dir, "nodes");
- nodesDir.mkdir();
- BrooklynMementoPersisterToMultiFile.checkDirIsAccessible(nodesDir);
-
- masterWriter = new MementoFileWriterSync<String>(getFileForMaster(), serializer, tmpSuffix);
- changeLogWriter = new MementoFileWriterSync<String>(getFileForChangeLog(), MementoSerializer.NOOP, tmpSuffix);
-
- LOG.info("ManagementPlaneMemento-persister will use directory {}", dir);
- }
-
- @Override
- public void stop() {
- running = false;
- try {
- for (MementoFileWriterSync<?> writer : nodeWriters.values()) {
- try {
- writer.waitForWriteCompleted(SHUTDOWN_TIMEOUT);
- } catch (TimeoutException e) {
- LOG.warn("Timeout during shutdown, waiting for write of "+writer+"; continuing");
- }
- }
- try {
- masterWriter.waitForWriteCompleted(SHUTDOWN_TIMEOUT);
- } catch (TimeoutException e) {
- LOG.warn("Timeout during shutdown, waiting for write of "+masterWriter+"; continuing");
- }
- } catch (InterruptedException e) {
- throw Exceptions.propagate(e);
- }
- }
-
- @VisibleForTesting
- public File getDir() {
- return dir;
- }
-
- @Override
- public ManagementPlaneSyncRecord loadSyncRecord() throws IOException {
- if (!running) {
- throw new IllegalStateException("Persister not running; cannot load memento from "+dir);
- }
-
- // Note this is called a lot - every time we check the heartbeats
- if (LOG.isTraceEnabled()) LOG.trace("Loading management-plane memento from {}", dir);
-
- Stopwatch stopwatch = Stopwatch.createStarted();
-
- ManagementPlaneSyncRecordImpl.Builder builder = ManagementPlaneSyncRecordImpl.builder();
-
- // Be careful about order: if the master-file says nodeX then nodeX's file must have an up-to-date timestamp.
- // Therefore read master file first, followed by the other node-files.
- File masterFile = getFileForMaster();
- String masterNodeId = (String) (masterFile.exists() ? serializer.fromString(readFile(masterFile)) : null);
- if (masterNodeId == null) {
- LOG.warn("No entity-memento deserialized from file "+masterFile+"; ignoring and continuing");
- } else {
- builder.masterNodeId(masterNodeId);
- }
-
- // Load node-files
- FileFilter fileFilter = new FileFilter() {
- @Override public boolean accept(File file) {
- return !file.getName().endsWith(".tmp");
- }
- };
- File[] nodeFiles = nodesDir.listFiles(fileFilter);
-
- for (File file : nodeFiles) {
- ManagementNodeSyncRecord memento = (ManagementNodeSyncRecord) serializer.fromString(readFile(file));
- if (memento == null) {
- LOG.warn("No manager-memento deserialized from file "+file+" (possibly just stopped?); ignoring and continuing");
- } else {
- builder.node(memento);
- }
- }
-
- if (LOG.isTraceEnabled()) LOG.trace("Loaded management-plane memento; took {}", Time.makeTimeStringRounded(stopwatch.elapsed(TimeUnit.MILLISECONDS)));
- return builder.build();
- }
-
- @Override
- public void delta(Delta delta) {
- if (!running) {
- if (LOG.isDebugEnabled()) LOG.debug("Persister not running; ignoring checkpointed delta of manager-memento");
- return;
- }
- if (LOG.isTraceEnabled()) LOG.trace("Checkpointed delta of manager-memento; updating {}", delta);
-
- for (ManagementNodeSyncRecord m : delta.getNodes()) {
- persist(m);
- }
- for (String id : delta.getRemovedNodeIds()) {
- deleteNode(id);
- }
- switch (delta.getMasterChange()) {
- case NO_CHANGE:
- break; // no-op
- case SET_MASTER:
- persistMaster(checkNotNull(delta.getNewMasterOrNull()));
- break;
- case CLEAR_MASTER:
- persistMaster(null);
- break; // no-op
- default:
- throw new IllegalStateException("Unknown state for master-change: "+delta.getMasterChange());
- }
- }
-
- private void persistMaster(String nodeId) {
- masterWriter.write(nodeId);
- changeLogWriter.append(Time.makeDateString()+": set master to "+nodeId+"\n");
- }
-
- @Override
- @VisibleForTesting
- public void waitForWritesCompleted(Duration timeout) throws InterruptedException, TimeoutException {
- for (MementoFileWriterSync<?> writer : nodeWriters.values()) {
- writer.waitForWriteCompleted(timeout);
- }
- masterWriter.waitForWriteCompleted(timeout);
- }
-
- private String readFile(File file) throws IOException {
- return Files.asCharSource(file, Charsets.UTF_8).read();
- }
-
- private void persist(ManagementNodeSyncRecord node) {
- MementoFileWriterSync<ManagementNodeSyncRecord> writer = getOrCreateNodeWriter(node.getNodeId());
- boolean fileExists = writer.exists();
- writer.write(node);
- if (!fileExists) {
- changeLogWriter.append(Time.makeDateString()+": created node "+node.getNodeId()+"\n");
- }
- if (node.getStatus() == ManagementNodeState.TERMINATED || node.getStatus() == ManagementNodeState.FAILED) {
- changeLogWriter.append(Time.makeDateString()+": set node "+node.getNodeId()+" status to "+node.getStatus()+"\n");
- }
- }
-
- private void deleteNode(String nodeId) {
- getOrCreateNodeWriter(nodeId).delete();
- changeLogWriter.append(Time.makeDateString()+": deleted node "+nodeId+"\n");
- }
-
- private MementoFileWriterSync<ManagementNodeSyncRecord> getOrCreateNodeWriter(String nodeId) {
- MementoFileWriterSync<ManagementNodeSyncRecord> writer = nodeWriters.get(nodeId);
- if (writer == null) {
- nodeWriters.putIfAbsent(nodeId, new MementoFileWriterSync<ManagementNodeSyncRecord>(getFileForNode(nodeId), serializer, tmpSuffix));
- writer = nodeWriters.get(nodeId);
- }
- return writer;
- }
-
- private File getFileForNode(String nodeId) {
- return new File(nodesDir, nodeId);
- }
-
- private File getFileForMaster() {
- return new File(dir, "master");
- }
-
- private File getFileForChangeLog() {
- return new File(dir, "change.log");
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/358806da/core/src/test/java/brooklyn/entity/rebind/persister/BrooklynMementoPersisterToMultiFileTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/rebind/persister/BrooklynMementoPersisterToMultiFileTest.java b/core/src/test/java/brooklyn/entity/rebind/persister/BrooklynMementoPersisterToMultiFileTest.java
deleted file mode 100644
index 6dc9aa1a..0000000
--- a/core/src/test/java/brooklyn/entity/rebind/persister/BrooklynMementoPersisterToMultiFileTest.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package brooklyn.entity.rebind.persister;
-
-import java.io.File;
-
-import org.testng.annotations.AfterMethod;
-import org.testng.annotations.Test;
-
-import brooklyn.entity.rebind.PersistenceExceptionHandlerImpl;
-import brooklyn.entity.rebind.RebindManagerImpl;
-import brooklyn.management.internal.LocalManagementContext;
-import brooklyn.test.entity.LocalManagementContextForTests;
-import brooklyn.util.javalang.JavaClassNames;
-import brooklyn.util.os.Os;
-import brooklyn.util.time.Duration;
-
-/**
- * @author Andrea Turli
- * @deprecated just tests the deprecated {@link BrooklynMementoPersisterToMultiFile}
- */
-public class BrooklynMementoPersisterToMultiFileTest extends BrooklynMementoPersisterTestFixture {
-
- protected File mementoDir;
-
- @Override
- protected LocalManagementContext newPersistingManagementContext() {
- mementoDir = Os.newTempDir(JavaClassNames.cleanSimpleClassName(this));
- Os.deleteOnExitRecursively(mementoDir);
-
- LocalManagementContext mgmt = new LocalManagementContextForTests();
- ((RebindManagerImpl) mgmt.getRebindManager()).setPeriodicPersistPeriod(Duration.millis(100));
- persister = new BrooklynMementoPersisterToMultiFile(mementoDir, BrooklynMementoPersisterToMultiFileTest.class.getClassLoader());
- mgmt.getRebindManager().setPersister(persister, PersistenceExceptionHandlerImpl.builder().build());
- mgmt.getHighAvailabilityManager().disabled();
- mgmt.getRebindManager().startPersistence();
- return mgmt;
- }
-
- @AfterMethod(alwaysRun=true)
- public void tearDown() throws Exception {
- super.tearDown();
- mementoDir = Os.deleteRecursively(mementoDir).asNullOrThrowing();
- }
-
- @Test(groups="Integration")
- public void testLoadAndCheckpointRawMemento() throws Exception {
- // test here is deliberately no-op;
- // checkpoint routines not supported for this (deprecated) persister
- }
-
-}