提交 eff261ef 编写于 作者: G Gary Yao

[FLINK-11156][tests, runtime] Simplify ZooKeeperCompletedCheckpointStore constructor

上级 02078e0e
...@@ -18,22 +18,17 @@ ...@@ -18,22 +18,17 @@
package org.apache.flink.runtime.checkpoint; package org.apache.flink.runtime.checkpoint;
import org.apache.flink.annotation.VisibleForTesting;
import org.apache.flink.api.java.tuple.Tuple2; import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.runtime.jobgraph.JobStatus; import org.apache.flink.runtime.jobgraph.JobStatus;
import org.apache.flink.runtime.jobmanager.HighAvailabilityMode; import org.apache.flink.runtime.jobmanager.HighAvailabilityMode;
import org.apache.flink.runtime.state.RetrievableStateHandle; import org.apache.flink.runtime.state.RetrievableStateHandle;
import org.apache.flink.runtime.zookeeper.RetrievableStateStorageHelper;
import org.apache.flink.runtime.zookeeper.ZooKeeperStateHandleStore; import org.apache.flink.runtime.zookeeper.ZooKeeperStateHandleStore;
import org.apache.flink.util.FlinkException; import org.apache.flink.util.FlinkException;
import org.apache.flink.util.function.ThrowingConsumer; import org.apache.flink.util.function.ThrowingConsumer;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.utils.ZKPaths;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -75,9 +70,6 @@ public class ZooKeeperCompletedCheckpointStore implements CompletedCheckpointSto ...@@ -75,9 +70,6 @@ public class ZooKeeperCompletedCheckpointStore implements CompletedCheckpointSto
private static final Comparator<Tuple2<RetrievableStateHandle<CompletedCheckpoint>, String>> STRING_COMPARATOR = Comparator.comparing(o -> o.f1); private static final Comparator<Tuple2<RetrievableStateHandle<CompletedCheckpoint>, String>> STRING_COMPARATOR = Comparator.comparing(o -> o.f1);
/** Curator ZooKeeper client. */
private final CuratorFramework client;
/** Completed checkpoints in ZooKeeper. */ /** Completed checkpoints in ZooKeeper. */
private final ZooKeeperStateHandleStore<CompletedCheckpoint> checkpointsInZooKeeper; private final ZooKeeperStateHandleStore<CompletedCheckpoint> checkpointsInZooKeeper;
...@@ -100,79 +92,23 @@ public class ZooKeeperCompletedCheckpointStore implements CompletedCheckpointSto ...@@ -100,79 +92,23 @@ public class ZooKeeperCompletedCheckpointStore implements CompletedCheckpointSto
* least 1). Adding more checkpoints than this results * least 1). Adding more checkpoints than this results
* in older checkpoints being discarded. On recovery, * in older checkpoints being discarded. On recovery,
* we will only start with a single checkpoint. * we will only start with a single checkpoint.
* @param client The Curator ZooKeeper client * @param checkpointsInZooKeeper Completed checkpoints in ZooKeeper
* @param checkpointsPath The ZooKeeper path for the checkpoints (needs to * @param executor to execute blocking calls
* start with a '/')
* @param stateStorage State storage to be used to persist the completed
* checkpoint
* @param executor to execute blocking calls
* @throws Exception
*/ */
public ZooKeeperCompletedCheckpointStore( public ZooKeeperCompletedCheckpointStore(
int maxNumberOfCheckpointsToRetain, int maxNumberOfCheckpointsToRetain,
CuratorFramework client, ZooKeeperStateHandleStore<CompletedCheckpoint> checkpointsInZooKeeper,
String checkpointsPath, Executor executor) {
RetrievableStateStorageHelper<CompletedCheckpoint> stateStorage,
Executor executor
) throws Exception {
this(maxNumberOfCheckpointsToRetain,
adaptNameSpace(client, checkpointsPath),
stateStorage,
executor);
LOG.info("Initialized in '{}'.", checkpointsPath);
}
@VisibleForTesting
ZooKeeperCompletedCheckpointStore(
int maxNumberOfCheckpointsToRetain,
CuratorFramework client,
String checkpointsPath,
Executor executor,
ZooKeeperStateHandleStore<CompletedCheckpoint> checkpointsInZooKeeper
) throws Exception {
this(maxNumberOfCheckpointsToRetain,
adaptNameSpace(client, checkpointsPath),
executor,
checkpointsInZooKeeper);
LOG.info("Initialized in '{}'.", checkpointsPath);
}
private ZooKeeperCompletedCheckpointStore(
int maxNumberOfCheckpointsToRetain,
CuratorFramework client,
RetrievableStateStorageHelper<CompletedCheckpoint> stateStorage,
Executor executor
) {
this(maxNumberOfCheckpointsToRetain,
client,
executor,
new ZooKeeperStateHandleStore<>(client, stateStorage));
}
private ZooKeeperCompletedCheckpointStore(
int maxNumberOfCheckpointsToRetain,
@Nonnull CuratorFramework client,
@Nonnull Executor executor,
@Nonnull ZooKeeperStateHandleStore<CompletedCheckpoint> checkpointsInZooKeeper
) {
checkArgument(maxNumberOfCheckpointsToRetain >= 1, "Must retain at least one checkpoint."); checkArgument(maxNumberOfCheckpointsToRetain >= 1, "Must retain at least one checkpoint.");
this.maxNumberOfCheckpointsToRetain = maxNumberOfCheckpointsToRetain; this.maxNumberOfCheckpointsToRetain = maxNumberOfCheckpointsToRetain;
this.client = client;
this.executor = executor;
this.checkpointsInZooKeeper = checkpointsInZooKeeper;
this.completedCheckpoints = new ArrayDeque<>(maxNumberOfCheckpointsToRetain + 1);
}
private static CuratorFramework adaptNameSpace(CuratorFramework client, String checkpointsPath) throws Exception { this.checkpointsInZooKeeper = checkNotNull(checkpointsInZooKeeper);
// Ensure that the checkpoints path exists
client.newNamespaceAwareEnsurePath(checkpointsPath) this.completedCheckpoints = new ArrayDeque<>(maxNumberOfCheckpointsToRetain + 1);
.ensure(client.getZookeeperClient());
// All operations will have the path as root this.executor = checkNotNull(executor);
return client.usingNamespace(client.getNamespace() + checkpointsPath);
} }
@Override @Override
...@@ -345,11 +281,7 @@ public class ZooKeeperCompletedCheckpointStore implements CompletedCheckpointSto ...@@ -345,11 +281,7 @@ public class ZooKeeperCompletedCheckpointStore implements CompletedCheckpointSto
} }
completedCheckpoints.clear(); completedCheckpoints.clear();
checkpointsInZooKeeper.deleteChildren();
String path = "/" + client.getNamespace();
LOG.info("Removing {} from ZooKeeper", path);
ZKPaths.deleteChildren(client.getZookeeperClient().getZooKeeper(), path, true);
} else { } else {
LOG.info("Suspending"); LOG.info("Suspending");
......
...@@ -294,12 +294,31 @@ public class ZooKeeperUtils { ...@@ -294,12 +294,31 @@ public class ZooKeeperUtils {
checkpointsPath += ZooKeeperSubmittedJobGraphStore.getPathForJob(jobId); checkpointsPath += ZooKeeperSubmittedJobGraphStore.getPathForJob(jobId);
return new ZooKeeperCompletedCheckpointStore( final ZooKeeperCompletedCheckpointStore zooKeeperCompletedCheckpointStore = new ZooKeeperCompletedCheckpointStore(
maxNumberOfCheckpointsToRetain, maxNumberOfCheckpointsToRetain,
client, createZooKeeperStateHandleStore(client, checkpointsPath, stateStorage),
checkpointsPath,
stateStorage,
executor); executor);
LOG.info("Initialized {} in '{}'.", ZooKeeperCompletedCheckpointStore.class.getSimpleName(), checkpointsPath);
return zooKeeperCompletedCheckpointStore;
}
/**
* Creates an instance of {@link ZooKeeperStateHandleStore}.
*
* @param client ZK client
* @param path Path to use for the client namespace
* @param stateStorage RetrievableStateStorageHelper that persist the actual state and whose
* returned state handle is then written to ZooKeeper
* @param <T> Type of state
* @return {@link ZooKeeperStateHandleStore} instance
* @throws Exception ZK errors
*/
public static <T extends Serializable> ZooKeeperStateHandleStore<T> createZooKeeperStateHandleStore(
final CuratorFramework client,
final String path,
final RetrievableStateStorageHelper<T> stateStorage) throws Exception {
return new ZooKeeperStateHandleStore<>(useNamespaceAndEnsurePath(client, path), stateStorage);
} }
/** /**
...@@ -362,6 +381,27 @@ public class ZooKeeperUtils { ...@@ -362,6 +381,27 @@ public class ZooKeeperUtils {
return root + namespace; return root + namespace;
} }
/**
* Returns a facade of the client that uses the specified namespace, and ensures that all nodes
* in the path exist.
*
* @param client ZK client
* @param path the new namespace
* @return ZK Client that uses the new namespace
* @throws Exception ZK errors
*/
public static CuratorFramework useNamespaceAndEnsurePath(final CuratorFramework client, final String path) throws Exception {
Preconditions.checkNotNull(client, "client must not be null");
Preconditions.checkNotNull(path, "path must not be null");
// Ensure that the checkpoints path exists
client.newNamespaceAwareEnsurePath(path)
.ensure(client.getZookeeperClient());
// All operations will have the path as root
return client.usingNamespace(generateZookeeperPath(client.getNamespace(), path));
}
/** /**
* Secure {@link ACLProvider} implementation. * Secure {@link ACLProvider} implementation.
*/ */
......
...@@ -24,6 +24,7 @@ import org.apache.flink.util.ExceptionUtils; ...@@ -24,6 +24,7 @@ import org.apache.flink.util.ExceptionUtils;
import org.apache.flink.util.InstantiationUtil; import org.apache.flink.util.InstantiationUtil;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.utils.ZKPaths;
import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.data.Stat; import org.apache.zookeeper.data.Stat;
...@@ -424,6 +425,17 @@ public class ZooKeeperStateHandleStore<T extends Serializable> { ...@@ -424,6 +425,17 @@ public class ZooKeeperStateHandleStore<T extends Serializable> {
} }
} }
/**
* Recursively deletes all children.
*
* @throws Exception ZK errors
*/
public void deleteChildren() throws Exception {
final String path = "/" + client.getNamespace();
LOG.info("Removing {} from ZooKeeper", path);
ZKPaths.deleteChildren(client.getZookeeperClient().getZooKeeper(), path, true);
}
// --------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------
// Protected methods // Protected methods
// --------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------
......
...@@ -80,13 +80,10 @@ public class ZooKeeperUtilityFactory { ...@@ -80,13 +80,10 @@ public class ZooKeeperUtilityFactory {
String zkStateHandleStorePath, String zkStateHandleStorePath,
RetrievableStateStorageHelper<T> stateStorageHelper) throws Exception { RetrievableStateStorageHelper<T> stateStorageHelper) throws Exception {
facade.newNamespaceAwareEnsurePath(zkStateHandleStorePath).ensure(facade.getZookeeperClient()); return ZooKeeperUtils.createZooKeeperStateHandleStore(
CuratorFramework stateHandleStoreFacade = facade.usingNamespace( facade,
ZooKeeperUtils.generateZookeeperPath( zkStateHandleStorePath,
facade.getNamespace(), stateStorageHelper);
zkStateHandleStorePath));
return new ZooKeeperStateHandleStore<>(stateHandleStoreFacade, stateStorageHelper);
} }
/** /**
......
...@@ -22,7 +22,9 @@ import org.apache.flink.runtime.concurrent.Executors; ...@@ -22,7 +22,9 @@ import org.apache.flink.runtime.concurrent.Executors;
import org.apache.flink.runtime.jobgraph.JobStatus; import org.apache.flink.runtime.jobgraph.JobStatus;
import org.apache.flink.runtime.state.RetrievableStateHandle; import org.apache.flink.runtime.state.RetrievableStateHandle;
import org.apache.flink.runtime.state.SharedStateRegistry; import org.apache.flink.runtime.state.SharedStateRegistry;
import org.apache.flink.runtime.util.ZooKeeperUtils;
import org.apache.flink.runtime.zookeeper.RetrievableStateStorageHelper; import org.apache.flink.runtime.zookeeper.RetrievableStateStorageHelper;
import org.apache.flink.runtime.zookeeper.ZooKeeperStateHandleStore;
import org.apache.flink.runtime.zookeeper.ZooKeeperTestEnvironment; import org.apache.flink.runtime.zookeeper.ZooKeeperTestEnvironment;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
...@@ -67,10 +69,14 @@ public class ZooKeeperCompletedCheckpointStoreITCase extends CompletedCheckpoint ...@@ -67,10 +69,14 @@ public class ZooKeeperCompletedCheckpointStoreITCase extends CompletedCheckpoint
@Override @Override
protected ZooKeeperCompletedCheckpointStore createCompletedCheckpoints(int maxNumberOfCheckpointsToRetain) throws Exception { protected ZooKeeperCompletedCheckpointStore createCompletedCheckpoints(int maxNumberOfCheckpointsToRetain) throws Exception {
return new ZooKeeperCompletedCheckpointStore(maxNumberOfCheckpointsToRetain, final ZooKeeperStateHandleStore<CompletedCheckpoint> checkpointsInZooKeeper = ZooKeeperUtils.createZooKeeperStateHandleStore(
ZOOKEEPER.getClient(), ZOOKEEPER.getClient(),
CHECKPOINT_PATH, CHECKPOINT_PATH,
new HeapStateStorageHelper(), new TestingRetrievableStateStorageHelper<>());
return new ZooKeeperCompletedCheckpointStore(
maxNumberOfCheckpointsToRetain,
checkpointsInZooKeeper,
Executors.directExecutor()); Executors.directExecutor());
} }
......
...@@ -157,14 +157,10 @@ public class ZooKeeperCompletedCheckpointStoreMockitoTest extends TestLogger { ...@@ -157,14 +157,10 @@ public class ZooKeeperCompletedCheckpointStoreMockitoTest extends TestLogger {
} }
}); });
final String checkpointsPath = "foobar";
ZooKeeperCompletedCheckpointStore zooKeeperCompletedCheckpointStore = new ZooKeeperCompletedCheckpointStore( ZooKeeperCompletedCheckpointStore zooKeeperCompletedCheckpointStore = new ZooKeeperCompletedCheckpointStore(
numCheckpointsToRetain, numCheckpointsToRetain,
client, zooKeeperStateHandleStoreMock,
checkpointsPath, Executors.directExecutor());
Executors.directExecutor(),
zooKeeperStateHandleStoreMock);
zooKeeperCompletedCheckpointStore.recover(); zooKeeperCompletedCheckpointStore.recover();
...@@ -221,14 +217,11 @@ public class ZooKeeperCompletedCheckpointStoreMockitoTest extends TestLogger { ...@@ -221,14 +217,11 @@ public class ZooKeeperCompletedCheckpointStoreMockitoTest extends TestLogger {
doThrow(new Exception()).when(zookeeperStateHandleStoreMock).releaseAndTryRemove(anyString()); doThrow(new Exception()).when(zookeeperStateHandleStoreMock).releaseAndTryRemove(anyString());
final int numCheckpointsToRetain = 1; final int numCheckpointsToRetain = 1;
final String checkpointsPath = "foobar";
ZooKeeperCompletedCheckpointStore zooKeeperCompletedCheckpointStore = new ZooKeeperCompletedCheckpointStore( ZooKeeperCompletedCheckpointStore zooKeeperCompletedCheckpointStore = new ZooKeeperCompletedCheckpointStore(
numCheckpointsToRetain, numCheckpointsToRetain,
client, zookeeperStateHandleStoreMock,
checkpointsPath, Executors.directExecutor());
Executors.directExecutor(),
zookeeperStateHandleStoreMock);
for (long i = 0; i <= numCheckpointsToRetain; ++i) { for (long i = 0; i <= numCheckpointsToRetain; ++i) {
CompletedCheckpoint checkpointToAdd = mock(CompletedCheckpoint.class); CompletedCheckpoint checkpointToAdd = mock(CompletedCheckpoint.class);
......
...@@ -25,6 +25,7 @@ import org.apache.flink.runtime.jobgraph.JobStatus; ...@@ -25,6 +25,7 @@ import org.apache.flink.runtime.jobgraph.JobStatus;
import org.apache.flink.runtime.state.SharedStateRegistry; import org.apache.flink.runtime.state.SharedStateRegistry;
import org.apache.flink.runtime.util.ZooKeeperUtils; import org.apache.flink.runtime.util.ZooKeeperUtils;
import org.apache.flink.runtime.zookeeper.ZooKeeperResource; import org.apache.flink.runtime.zookeeper.ZooKeeperResource;
import org.apache.flink.runtime.zookeeper.ZooKeeperStateHandleStore;
import org.apache.flink.util.TestLogger; import org.apache.flink.util.TestLogger;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
...@@ -117,11 +118,14 @@ public class ZooKeeperCompletedCheckpointStoreTest extends TestLogger { ...@@ -117,11 +118,14 @@ public class ZooKeeperCompletedCheckpointStoreTest extends TestLogger {
@Nonnull @Nonnull
private ZooKeeperCompletedCheckpointStore createZooKeeperCheckpointStore(CuratorFramework client) throws Exception { private ZooKeeperCompletedCheckpointStore createZooKeeperCheckpointStore(CuratorFramework client) throws Exception {
return new ZooKeeperCompletedCheckpointStore( final ZooKeeperStateHandleStore<CompletedCheckpoint> checkpointsInZooKeeper = ZooKeeperUtils.createZooKeeperStateHandleStore(
1,
client, client,
"/checkpoints", "/checkpoints",
new TestingRetrievableStateStorageHelper<>(), new TestingRetrievableStateStorageHelper<>());
return new ZooKeeperCompletedCheckpointStore(
1,
checkpointsInZooKeeper,
Executors.directExecutor()); Executors.directExecutor());
} }
......
...@@ -42,10 +42,13 @@ import java.util.HashSet; ...@@ -42,10 +42,13 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
...@@ -685,6 +688,18 @@ public class ZooKeeperStateHandleStoreTest extends TestLogger { ...@@ -685,6 +688,18 @@ public class ZooKeeperStateHandleStoreTest extends TestLogger {
assertEquals(0, stat.getNumChildren()); assertEquals(0, stat.getNumChildren());
} }
@Test
public void testDeleteAllShouldRemoveAllPaths() throws Exception {
final ZooKeeperStateHandleStore<Long> zkStore = new ZooKeeperStateHandleStore<>(
ZooKeeperUtils.useNamespaceAndEnsurePath(ZOOKEEPER.getClient(), "/path"),
new LongStateStorage());
zkStore.addAndLock("/state", 1L);
zkStore.deleteChildren();
assertThat(zkStore.getAllPaths(), is(empty()));
}
// --------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------
// Simple test helpers // Simple test helpers
// --------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册