Skip to content

Commit

Permalink
fixup! CURATOR-718: Refactor CuratorFramework inheritance hierarchy b…
Browse files Browse the repository at this point in the history
…y composing functionalities

Notable changes to last review:
1. Rename `InternalCuratorFramework` to `CuratorFrameworkBase`.
2. Make `DelegatingCuratorFramework` `abstract` and package `private`.
3. Move `start`/`close` from `DelegatingCuratorFramework` to subclasses.
  • Loading branch information
kezhuw committed Feb 7, 2025
1 parent c47cc6f commit 8c37651
Show file tree
Hide file tree
Showing 52 changed files with 170 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@
import org.apache.zookeeper.Watcher;

public class AddWatchBuilderImpl implements AddWatchBuilder, Pathable<Void>, BackgroundOperation<String> {
private final InternalCuratorFramework client;
private final CuratorFrameworkBase client;
private Watching watching;
private Backgrounding backgrounding = new Backgrounding();
private AddWatchMode mode = AddWatchMode.PERSISTENT_RECURSIVE;

AddWatchBuilderImpl(InternalCuratorFramework client) {
AddWatchBuilderImpl(CuratorFrameworkBase client) {
this.client = client;
watching = new Watching(client, true);
}

public AddWatchBuilderImpl(
InternalCuratorFramework client, Watching watching, Backgrounding backgrounding, AddWatchMode mode) {
CuratorFrameworkBase client, Watching watching, Backgrounding backgrounding, AddWatchMode mode) {
this.client = client;
this.watching = watching;
this.backgrounding = backgrounding;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import org.apache.zookeeper.AsyncCallback;

class BackgroundSyncImpl implements BackgroundOperation<String> {
private final InternalCuratorFramework client;
private final CuratorFrameworkBase client;
private final Object context;

BackgroundSyncImpl(InternalCuratorFramework client, Object context) {
BackgroundSyncImpl(CuratorFrameworkBase client, Object context) {
this.client = client;
this.context = context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ public class Backgrounding {
errorListener = null;
}

Backgrounding(InternalCuratorFramework client, BackgroundCallback callback, Object context, Executor executor) {
Backgrounding(CuratorFrameworkBase client, BackgroundCallback callback, Object context, Executor executor) {
this(wrapCallback(client, callback, executor), context);
}

Backgrounding(InternalCuratorFramework client, BackgroundCallback callback, Executor executor) {
Backgrounding(CuratorFrameworkBase client, BackgroundCallback callback, Executor executor) {
this(wrapCallback(client, callback, executor));
}

Expand Down Expand Up @@ -119,7 +119,7 @@ void checkError(Throwable e, Watching watching) throws Exception {
}

private static BackgroundCallback wrapCallback(
final InternalCuratorFramework client, final BackgroundCallback callback, final Executor executor) {
final CuratorFrameworkBase client, final BackgroundCallback callback, final Executor executor) {
return new BackgroundCallback() {
@Override
public void processResult(CuratorFramework dummy, final CuratorEvent event) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class CreateBuilderImpl
BackgroundOperation<PathAndBytes>,
ErrorListenerPathAndBytesable<String> {
private final Logger log = LoggerFactory.getLogger(getClass());
private final InternalCuratorFramework client;
private final CuratorFrameworkBase client;
private final ProtectedMode protectedMode = new ProtectedMode();
private CreateMode createMode;
private Backgrounding backgrounding;
Expand All @@ -93,7 +93,7 @@ public class CreateBuilderImpl
@VisibleForTesting
boolean failNextIdempotentCheckForTesting = false;

CreateBuilderImpl(InternalCuratorFramework client) {
CreateBuilderImpl(CuratorFrameworkBase client) {
this.client = client;
createMode = CreateMode.PERSISTENT;
backgrounding = new Backgrounding();
Expand All @@ -107,7 +107,7 @@ public class CreateBuilderImpl
}

public CreateBuilderImpl(
InternalCuratorFramework client,
CuratorFrameworkBase client,
CreateMode createMode,
Backgrounding backgrounding,
boolean createParentsIfNeeded,
Expand Down Expand Up @@ -737,7 +737,7 @@ public ProtectACLCreateModePathAndBytesable<String> creatingParentContainersIfNe
}

static <T> void backgroundCreateParentsThenNode(
final InternalCuratorFramework client,
final CuratorFrameworkBase client,
final OperationAndData<T> mainOperationAndData,
final String path,
final InternalACLProvider aclProvider,
Expand Down Expand Up @@ -766,7 +766,7 @@ public CuratorEventType getBackgroundEventType() {
}

private void backgroundSetData(
final InternalCuratorFramework client,
final CuratorFrameworkBase client,
final OperationAndData<PathAndBytes> mainOperationAndData,
final String path,
final Backgrounding backgrounding) {
Expand Down Expand Up @@ -801,7 +801,7 @@ public CuratorEventType getBackgroundEventType() {
}

private void backgroundCheckIdempotent(
final InternalCuratorFramework client,
final CuratorFrameworkBase client,
final OperationAndData<PathAndBytes> mainOperationAndData,
final String path,
final Backgrounding backgrounding) {
Expand Down Expand Up @@ -844,7 +844,7 @@ private void sendBackgroundResponse(
}

private static <T> void sendBackgroundResponse(
InternalCuratorFramework client,
CuratorFrameworkBase client,
int rc,
String path,
Object ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public String toString() {
}

CuratorEventImpl(
InternalCuratorFramework client,
CuratorFrameworkBase client,
CuratorEventType type,
int resultCode,
String path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@
import org.apache.zookeeper.ZooKeeper;

/**
* This is the internal version of {@link CuratorFramework}.
* This is the base class of all {@link CuratorFramework}s, it is public for private usages (a.k.a. impls/details package).
*
* <p>Most internal codes should use {@link InternalCuratorFramework} instead of {@link CuratorFrameworkImpl}, so
* <p>Most internal codes should use {@link CuratorFrameworkBase} instead of {@link CuratorFrameworkImpl}, so
* functionalities could be added additively by overriding methods in {@link DelegatingCuratorFramework}.
*
* <p>An instance of {@link CuratorFramework} MUST BE an instance of {@link InternalCuratorFramework}.
* <p>An instance of {@link CuratorFramework} MUST BE an instance of {@link CuratorFrameworkBase}.
*/
public abstract class InternalCuratorFramework implements CuratorFramework {
public abstract class CuratorFrameworkBase implements CuratorFramework {
abstract NamespaceImpl getNamespaceImpl();

@Override
Expand Down Expand Up @@ -111,7 +111,7 @@ final ZooKeeper getZooKeeper() throws Exception {
return getZookeeperClient().getZooKeeper();
}

protected final void internalSync(InternalCuratorFramework impl, String path, Object context) {
protected final void internalSync(CuratorFrameworkBase impl, String path, Object context) {
BackgroundOperation<String> operation = new BackgroundSyncImpl(impl, context);
processBackgroundOperation(new OperationAndData(operation, path, null, null, context, null), null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class CuratorFrameworkImpl extends InternalCuratorFramework {
public final class CuratorFrameworkImpl extends CuratorFrameworkBase {
private final Logger log = LoggerFactory.getLogger(getClass());
private final CuratorZookeeperClient client;
private final StandardListenerManager<CuratorListener> listeners;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public class CuratorMultiTransactionImpl
CuratorMultiTransactionMain,
BackgroundOperation<CuratorMultiTransactionRecord>,
ErrorListenerMultiTransactionMain {
private final InternalCuratorFramework client;
private final CuratorFrameworkBase client;
private Backgrounding backgrounding = new Backgrounding();

public CuratorMultiTransactionImpl(InternalCuratorFramework client) {
public CuratorMultiTransactionImpl(CuratorFrameworkBase client) {
this.client = client;
}

public CuratorMultiTransactionImpl(InternalCuratorFramework client, Backgrounding backgrounding) {
public CuratorMultiTransactionImpl(CuratorFrameworkBase client, Backgrounding backgrounding) {
this.client = client;
this.backgrounding = backgrounding;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class CuratorTempFrameworkImpl implements CuratorTempFramework {
private final long inactiveThresholdMs;

// guarded by sync
private InternalCuratorFramework client;
private CuratorFrameworkBase client;

// guarded by sync
private ScheduledExecutorService cleanup;
Expand Down Expand Up @@ -67,7 +67,7 @@ public TempGetDataBuilder getData() throws Exception {
}

@VisibleForTesting
synchronized InternalCuratorFramework getClient() {
synchronized CuratorFrameworkBase getClient() {
return client;
}

Expand All @@ -83,7 +83,7 @@ synchronized void updateLastAccess() {

private synchronized void openConnectionIfNeeded() throws Exception {
if (client == null) {
client = (InternalCuratorFramework) factory.build(); // cast is safe - we control both sides of this
client = (CuratorFrameworkBase) factory.build(); // cast is safe - we control both sides of this
client.start();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@

@SuppressWarnings("deprecation")
class CuratorTransactionImpl implements CuratorTransaction, CuratorTransactionBridge, CuratorTransactionFinal {
private final InternalCuratorFramework client;
private final CuratorFrameworkBase client;
private final CuratorMultiTransactionRecord transaction;

private boolean isCommitted = false;

CuratorTransactionImpl(InternalCuratorFramework client) {
CuratorTransactionImpl(CuratorFrameworkBase client) {
this.client = client;
transaction = new CuratorMultiTransactionRecord();
}
Expand Down Expand Up @@ -83,7 +83,7 @@ public TransactionCheckBuilder<CuratorTransactionBridge> check() {
}

static <T> TransactionCheckBuilder<T> makeTransactionCheckBuilder(
final InternalCuratorFramework client, final T context, final CuratorMultiTransactionRecord transaction) {
final CuratorFrameworkBase client, final T context, final CuratorMultiTransactionRecord transaction) {
return new TransactionCheckBuilder<T>() {
private int version = -1;

Expand Down Expand Up @@ -125,7 +125,7 @@ public List<OpResult> call() throws Exception {
}

static List<CuratorTransactionResult> wrapResults(
InternalCuratorFramework client, List<OpResult> resultList, CuratorMultiTransactionRecord transaction) {
CuratorFrameworkBase client, List<OpResult> resultList, CuratorMultiTransactionRecord transaction) {
ImmutableList.Builder<CuratorTransactionResult> builder = ImmutableList.builder();
for (int i = 0; i < resultList.size(); ++i) {
OpResult opResult = resultList.get(i);
Expand All @@ -138,7 +138,7 @@ static List<CuratorTransactionResult> wrapResults(
}

static CuratorTransactionResult makeCuratorResult(
InternalCuratorFramework client, OpResult opResult, TypeAndPath metadata) {
CuratorFrameworkBase client, OpResult opResult, TypeAndPath metadata) {
String resultPath = null;
Stat resultStat = null;
int error = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,17 @@
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier;

public class DelegatingCuratorFramework extends InternalCuratorFramework {
protected final InternalCuratorFramework client;
/**
* Delegates methods to shadowed {@link CuratorFrameworkBase} so subclasses can override only methods that need
* additional cares.
*/
abstract class DelegatingCuratorFramework extends CuratorFrameworkBase {
protected final CuratorFrameworkBase client;

public DelegatingCuratorFramework(InternalCuratorFramework client) {
public DelegatingCuratorFramework(CuratorFrameworkBase client) {
this.client = client;
}

@Override
public void start() {
throw new UnsupportedOperationException();
}

@Override
public void close() {
throw new UnsupportedOperationException();
}

@Override
public CuratorFrameworkState getState() {
return client.getState();
Expand Down Expand Up @@ -201,8 +195,7 @@ NamespaceFacadeCache getNamespaceFacadeCache() {
}

@Override
public <DATA_TYPE> void processBackgroundOperation(
OperationAndData<DATA_TYPE> operationAndData, CuratorEvent event) {
<DATA_TYPE> void processBackgroundOperation(OperationAndData<DATA_TYPE> operationAndData, CuratorEvent event) {
client.processBackgroundOperation(operationAndData, event);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.apache.zookeeper.Op;

public class DeleteBuilderImpl implements DeleteBuilder, BackgroundOperation<String>, ErrorListenerPathable<Void> {
private final InternalCuratorFramework client;
private final CuratorFrameworkBase client;
private int version;
private Backgrounding backgrounding;
private boolean deletingChildrenIfNeeded;
Expand All @@ -47,7 +47,7 @@ public class DeleteBuilderImpl implements DeleteBuilder, BackgroundOperation<Str
@VisibleForTesting
boolean failBeforeNextDeleteForTesting = false;

DeleteBuilderImpl(InternalCuratorFramework client) {
DeleteBuilderImpl(CuratorFrameworkBase client) {
this.client = client;
version = -1;
backgrounding = new Backgrounding();
Expand All @@ -57,7 +57,7 @@ public class DeleteBuilderImpl implements DeleteBuilder, BackgroundOperation<Str
}

public DeleteBuilderImpl(
InternalCuratorFramework client,
CuratorFrameworkBase client,
int version,
Backgrounding backgrounding,
boolean deletingChildrenIfNeeded,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@

public class ExistsBuilderImpl
implements ExistsBuilder, BackgroundOperation<String>, ErrorListenerPathable<Stat>, ACLableExistBuilderMain {
private final InternalCuratorFramework client;
private final CuratorFrameworkBase client;
private Backgrounding backgrounding;
private Watching watching;
private boolean createParentsIfNeeded;
private boolean createParentContainersIfNeeded;
private ACLing acling;

ExistsBuilderImpl(InternalCuratorFramework client) {
ExistsBuilderImpl(CuratorFrameworkBase client) {
this(client, new Backgrounding(), null, false, false);
}

public ExistsBuilderImpl(
InternalCuratorFramework client,
CuratorFrameworkBase client,
Backgrounding backgrounding,
Watcher watcher,
boolean createParentsIfNeeded,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@

class FindAndDeleteProtectedNodeInBackground implements BackgroundOperation<Void> {
private final Logger log = LoggerFactory.getLogger(getClass());
private final InternalCuratorFramework client;
private final CuratorFrameworkBase client;
private final String namespaceAdjustedParentPath;
private final String protectedId;

FindAndDeleteProtectedNodeInBackground(
InternalCuratorFramework client, String namespaceAdjustedParentPath, String protectedId) {
CuratorFrameworkBase client, String namespaceAdjustedParentPath, String protectedId) {
this.client = client;
this.namespaceAdjustedParentPath = namespaceAdjustedParentPath;
this.protectedId = protectedId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@
import org.apache.zookeeper.data.Stat;

public class GetACLBuilderImpl implements GetACLBuilder, BackgroundOperation<String>, ErrorListenerPathable<List<ACL>> {
private final InternalCuratorFramework client;
private final CuratorFrameworkBase client;

private Backgrounding backgrounding;
private Stat responseStat;

GetACLBuilderImpl(InternalCuratorFramework client) {
GetACLBuilderImpl(CuratorFrameworkBase client) {
this.client = client;
backgrounding = new Backgrounding();
responseStat = new Stat();
}

public GetACLBuilderImpl(InternalCuratorFramework client, Backgrounding backgrounding, Stat responseStat) {
public GetACLBuilderImpl(CuratorFrameworkBase client, Backgrounding backgrounding, Stat responseStat) {
this.client = client;
this.backgrounding = backgrounding;
this.responseStat = responseStat;
Expand Down
Loading

0 comments on commit 8c37651

Please sign in to comment.