Skip to content

Commit bcddf8e

Browse files
committed
JAVA-1801: Revisit NodeStateListener and SchemaChangeListener APIs
1 parent 546b28c commit bcddf8e

32 files changed

Lines changed: 430 additions & 375 deletions

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### 4.0.0-alpha4 (in progress)
66

7+
- [improvement] JAVA-1801: Revisit NodeStateListener and SchemaChangeListener APIs
78
- [improvement] JAVA-1759: Revisit metrics API
89
- [improvement] JAVA-1776: Use concurrency annotations
910
- [improvement] JAVA-1799: Use CqlIdentifier for simple statement named values

core/src/main/java/com/datastax/oss/driver/api/core/config/DefaultDriverOption.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ public enum DefaultDriverOption implements DriverOption {
113113
METADATA_SCHEMA_WINDOW("metadata.schema.debouncer.window", true),
114114
METADATA_SCHEMA_MAX_EVENTS("metadata.schema.debouncer.max-events", true),
115115
METADATA_TOKEN_MAP_ENABLED("metadata.token-map.enabled", true),
116+
METADATA_NODE_STATE_LISTENER_CLASS("metadata.node-state-listener.class", false),
117+
METADATA_SCHEMA_CHANGE_LISTENER_CLASS("metadata.schema-change-listener.class", false),
116118

117119
TIMESTAMP_GENERATOR_CLASS("request.timestamp-generator.class", true),
118120
TIMESTAMP_GENERATOR_FORCE_JAVA_CLOCK("request.timestamp-generator.force-java-clock", false),

core/src/main/java/com/datastax/oss/driver/api/core/metadata/NodeStateListener.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,23 @@
1616
package com.datastax.oss.driver.api.core.metadata;
1717

1818
import com.datastax.oss.driver.api.core.loadbalancing.NodeDistance;
19-
import com.datastax.oss.driver.api.core.session.Session;
2019
import com.datastax.oss.driver.api.core.session.SessionBuilder;
2120

2221
/**
2322
* A listener that gets notified when nodes states change.
2423
*
25-
* <p>An implementation of this interface can be registered with {@link
26-
* SessionBuilder#addNodeStateListeners(NodeStateListener...)} or at runtime with {@link
27-
* Session#register(NodeStateListener)}.
24+
* <p>An implementation of this interface can be registered in the configuration, or with {@link
25+
* SessionBuilder#withNodeStateListener(NodeStateListener)}.
2826
*
2927
* <p>Note that the methods defined by this interface will be executed by internal driver threads,
3028
* and are therefore expected to have short execution times. If you need to perform long
3129
* computations or blocking calls in response to schema change events, it is strongly recommended to
3230
* schedule them asynchronously on a separate thread provided by your application code.
31+
*
32+
* <p>If you implement this interface but don't need to implement all the methods, extend {@link
33+
* NodeStateListenerBase}.
3334
*/
34-
public interface NodeStateListener {
35+
public interface NodeStateListener extends AutoCloseable {
3536

3637
/**
3738
* Invoked when a node is first added to the cluster.
@@ -61,13 +62,4 @@ public interface NodeStateListener {
6162
* absent from the new list.
6263
*/
6364
void onRemove(Node node);
64-
65-
/** Invoked when the listener is registered with a session. */
66-
void onRegister(Session session);
67-
68-
/**
69-
* Invoked when the listener is unregistered from a session, or at session shutdown, whichever
70-
* comes first.
71-
*/
72-
void onUnregister(Session session);
7365
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright DataStax, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.oss.driver.api.core.metadata;
17+
18+
/**
19+
* Convenience class for listener implementations that that don't need to override all methods (all
20+
* methods in this class are empty).
21+
*/
22+
public class NodeStateListenerBase implements NodeStateListener {
23+
24+
@Override
25+
public void onAdd(Node node) {
26+
// nothing to do
27+
}
28+
29+
@Override
30+
public void onUp(Node node) {
31+
// nothing to do
32+
}
33+
34+
@Override
35+
public void onDown(Node node) {
36+
// nothing to do
37+
}
38+
39+
@Override
40+
public void onRemove(Node node) {
41+
// nothing to do
42+
}
43+
44+
@Override
45+
public void close() throws Exception {
46+
// nothing to do
47+
}
48+
}

core/src/main/java/com/datastax/oss/driver/api/core/metadata/schema/SchemaChangeListener.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,24 @@
1515
*/
1616
package com.datastax.oss.driver.api.core.metadata.schema;
1717

18-
import com.datastax.oss.driver.api.core.session.Session;
18+
import com.datastax.oss.driver.api.core.session.SessionBuilder;
1919
import com.datastax.oss.driver.api.core.type.UserDefinedType;
2020

2121
/**
2222
* Tracks schema changes.
2323
*
24-
* <p>An implementation of this interface can be registered with {@link
25-
* Session#register(SchemaChangeListener)}.
24+
* <p>An implementation of this interface can be registered in the configuration, or with {@link
25+
* SessionBuilder#withSchemaChangeListener(SchemaChangeListener)}.
2626
*
2727
* <p>Note that the methods defined by this interface will be executed by internal driver threads,
2828
* and are therefore expected to have short execution times. If you need to perform long
2929
* computations or blocking calls in response to schema change events, it is strongly recommended to
3030
* schedule them asynchronously on a separate thread provided by your application code.
31+
*
32+
* <p>If you implement this interface but don't need to implement all the methods, extend {@link
33+
* SchemaChangeListenerBase}.
3134
*/
32-
public interface SchemaChangeListener {
35+
public interface SchemaChangeListener extends AutoCloseable {
3336

3437
void onKeyspaceCreated(KeyspaceMetadata keyspace);
3538

@@ -66,13 +69,4 @@ public interface SchemaChangeListener {
6669
void onViewDropped(ViewMetadata view);
6770

6871
void onViewUpdated(ViewMetadata current, ViewMetadata previous);
69-
70-
/** Invoked when the listener is registered with a session. */
71-
void onRegister(Session session);
72-
73-
/**
74-
* Invoked when the listener is unregistered from a session, or at session shutdown, whichever
75-
* comes first.
76-
*/
77-
void onUnregister(Session session);
7872
}

core/src/main/java/com/datastax/oss/driver/api/core/metadata/schema/SchemaChangeListenerBase.java

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,74 +15,106 @@
1515
*/
1616
package com.datastax.oss.driver.api.core.metadata.schema;
1717

18-
import com.datastax.oss.driver.api.core.session.Session;
1918
import com.datastax.oss.driver.api.core.type.UserDefinedType;
2019

2120
/**
22-
* Convenience schema change listener implementation that defines all methods as no-ops.
23-
*
24-
* <p>Implementors that are only interested in a subset of events can extend this class and override
25-
* the relevant methods.
21+
* Convenience class for listener implementations that that don't need to override all methods (all
22+
* methods in this class are empty).
2623
*/
2724
public class SchemaChangeListenerBase implements SchemaChangeListener {
2825

2926
@Override
30-
public void onKeyspaceCreated(KeyspaceMetadata keyspace) {}
31-
32-
@Override
33-
public void onKeyspaceDropped(KeyspaceMetadata keyspace) {}
27+
public void onKeyspaceCreated(KeyspaceMetadata keyspace) {
28+
// nothing to do
29+
}
3430

3531
@Override
36-
public void onKeyspaceUpdated(KeyspaceMetadata current, KeyspaceMetadata previous) {}
32+
public void onKeyspaceDropped(KeyspaceMetadata keyspace) {
33+
// nothing to do
34+
}
3735

3836
@Override
39-
public void onTableCreated(TableMetadata table) {}
37+
public void onKeyspaceUpdated(KeyspaceMetadata current, KeyspaceMetadata previous) {
38+
// nothing to do
39+
}
4040

4141
@Override
42-
public void onTableDropped(TableMetadata table) {}
42+
public void onTableCreated(TableMetadata table) {
43+
// nothing to do
44+
}
4345

4446
@Override
45-
public void onTableUpdated(TableMetadata current, TableMetadata previous) {}
47+
public void onTableDropped(TableMetadata table) {
48+
// nothing to do
49+
}
4650

4751
@Override
48-
public void onUserDefinedTypeCreated(UserDefinedType type) {}
52+
public void onTableUpdated(TableMetadata current, TableMetadata previous) {
53+
// nothing to do
54+
}
4955

5056
@Override
51-
public void onUserDefinedTypeDropped(UserDefinedType type) {}
57+
public void onUserDefinedTypeCreated(UserDefinedType type) {
58+
// nothing to do
59+
}
5260

5361
@Override
54-
public void onUserDefinedTypeUpdated(UserDefinedType current, UserDefinedType previous) {}
62+
public void onUserDefinedTypeDropped(UserDefinedType type) {
63+
// nothing to do
64+
}
5565

5666
@Override
57-
public void onFunctionCreated(FunctionMetadata function) {}
67+
public void onUserDefinedTypeUpdated(UserDefinedType current, UserDefinedType previous) {
68+
// nothing to do
69+
}
5870

5971
@Override
60-
public void onFunctionDropped(FunctionMetadata function) {}
72+
public void onFunctionCreated(FunctionMetadata function) {
73+
// nothing to do
74+
}
6175

6276
@Override
63-
public void onFunctionUpdated(FunctionMetadata current, FunctionMetadata previous) {}
77+
public void onFunctionDropped(FunctionMetadata function) {
78+
// nothing to do
79+
}
6480

6581
@Override
66-
public void onAggregateCreated(AggregateMetadata aggregate) {}
82+
public void onFunctionUpdated(FunctionMetadata current, FunctionMetadata previous) {
83+
// nothing to do
84+
}
6785

6886
@Override
69-
public void onAggregateDropped(AggregateMetadata aggregate) {}
87+
public void onAggregateCreated(AggregateMetadata aggregate) {
88+
// nothing to do
89+
}
7090

7191
@Override
72-
public void onAggregateUpdated(AggregateMetadata current, AggregateMetadata previous) {}
92+
public void onAggregateDropped(AggregateMetadata aggregate) {
93+
// nothing to do
94+
}
7395

7496
@Override
75-
public void onViewCreated(ViewMetadata view) {}
97+
public void onAggregateUpdated(AggregateMetadata current, AggregateMetadata previous) {
98+
// nothing to do
99+
}
76100

77101
@Override
78-
public void onViewDropped(ViewMetadata view) {}
102+
public void onViewCreated(ViewMetadata view) {
103+
// nothing to do
104+
}
79105

80106
@Override
81-
public void onViewUpdated(ViewMetadata current, ViewMetadata previous) {}
107+
public void onViewDropped(ViewMetadata view) {
108+
// nothing to do
109+
}
82110

83111
@Override
84-
public void onRegister(Session session) {}
112+
public void onViewUpdated(ViewMetadata current, ViewMetadata previous) {
113+
// nothing to do
114+
}
85115

86116
@Override
87-
public void onUnregister(Session session) {}
117+
public void close() throws Exception {
118+
// nothing to do
119+
}
88120
}

core/src/main/java/com/datastax/oss/driver/api/core/session/Session.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import com.datastax.oss.driver.api.core.metadata.Metadata;
2525
import com.datastax.oss.driver.api.core.metadata.Node;
2626
import com.datastax.oss.driver.api.core.metadata.NodeState;
27-
import com.datastax.oss.driver.api.core.metadata.NodeStateListener;
28-
import com.datastax.oss.driver.api.core.metadata.schema.SchemaChangeListener;
2927
import com.datastax.oss.driver.api.core.metrics.Metrics;
3028
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
3129
import com.datastax.oss.driver.internal.core.util.concurrent.BlockingOperation;
@@ -195,32 +193,4 @@ default boolean checkSchemaAgreement() {
195193
*/
196194
<RequestT extends Request, ResultT> ResultT execute(
197195
RequestT request, GenericType<ResultT> resultType);
198-
199-
/**
200-
* Registers the provided schema change listener.
201-
*
202-
* <p>This is a no-op if the listener was registered already.
203-
*/
204-
void register(SchemaChangeListener listener);
205-
206-
/**
207-
* Unregisters the provided schema change listener.
208-
*
209-
* <p>This is a no-op if the listener was not registered.
210-
*/
211-
void unregister(SchemaChangeListener listener);
212-
213-
/**
214-
* Registers the provided node state listener.
215-
*
216-
* <p>This is a no-op if the listener was registered already.
217-
*/
218-
void register(NodeStateListener listener);
219-
220-
/**
221-
* Unregisters the provided node state listener.
222-
*
223-
* <p>This is a no-op if the listener was not registered.
224-
*/
225-
void unregister(NodeStateListener listener);
226196
}

0 commit comments

Comments
 (0)