Elffuss BraidHub

kikocis/java-driver public

Merge pull request #589 from datastax/java1011

JAVA-1011: Expose PoolingOptions defaults values.
Changeset13ff5c33b0a068591df0f79b4f2fe6201cc63e9ea09284984d4febb22ecf7b0f
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2016-02-23 17:46 +01:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2016-02-23 17:46 +01:00
Parents4358932e3bd3, 31fb7af28bd6

Browse the files at this changeset

2 files changed

modified changelog/README.md +1 −0
@@ -12,6 +12,7 @@
1212 - [bug] JAVA-1039: Minor bugs in Event Debouncer.
1313 - [improvement] JAVA-843: Disable frozen checks in mapper.
1414 - [improvement] JAVA-833: Improve message when a nested type can't be serialized.
15+- [improvement] JAVA-1011: Expose PoolingOptions default values.
1516
1617 Merged from 2.0 branch:
1718
modified driver-core/src/main/java/com/datastax/driver/core/PoolingOptions.java +83 −34
@@ -66,33 +66,76 @@
6666 */
6767 public static final int UNSET = Integer.MIN_VALUE;
6868
69- private static final Map<ProtocolVersion, Map<String, Integer>> DEFAULTS = ImmutableMap.<ProtocolVersion, Map<String, Integer>>of(
70- ProtocolVersion.V2, ImmutableMap.<String, Integer>builder()
71- .put("corePoolLocal", 2)
72- .put("maxPoolLocal", 8)
73- .put("corePoolRemote", 1)
74- .put("maxPoolRemote", 2)
75- .put("newConnectionThresholdLocal", 100)
76- .put("newConnectionThresholdRemote", 100)
77- .put("maxRequestsPerConnectionLocal", 128)
78- .put("maxRequestsPerConnectionRemote", 128)
69+ public static final String CORE_POOL_LOCAL_KEY = "corePoolLocal";
70+ public static final String MAX_POOL_LOCAL_KEY = "maxPoolLocal";
71+ public static final String CORE_POOL_REMOTE_KEY = "corePoolRemote";
72+ public static final String MAX_POOL_REMOTE_KEY = "maxPoolRemote";
73+ public static final String NEW_CONNECTION_THRESHOLD_LOCAL_KEY = "newConnectionThresholdLocal";
74+ public static final String NEW_CONNECTION_THRESHOLD_REMOTE_KEY = "newConnectionThresholdRemote";
75+ public static final String MAX_REQUESTS_PER_CONNECTION_LOCAL_KEY = "maxRequestsPerConnectionLocal";
76+ public static final String MAX_REQUESTS_PER_CONNECTION_REMOTE_KEY = "maxRequestsPerConnectionRemote";
77+
78+ /**
79+ * The default values for connection options, that depend on the native protocol version.
80+ * <p/>
81+ * The map stores protocol versions in ascending order, and only the versions that introduced a change are present.
82+ * To find the defaults for a particular version, look for the highest key that is less than or equal to that
83+ * version, in other words:
84+ * <pre>{@code
85+ * ProtocolVersion referenceVersion = null;
86+ * for (ProtocolVersion key : DEFAULTS.keySet()) {
87+ * if (key.compareTo(actualVersion) > 0)
88+ * break;
89+ * else
90+ * referenceVersion = key;
91+ * }
92+ * Map<String, Integer> defaults = DEFAULTS.get(referenceVersion);
93+ * }</pre>
94+ * Once you've extracted the underlying map, use the keys {@code CORE_POOL_LOCAL_KEY},
95+ * {@code MAX_POOL_LOCAL_KEY}, {@code CORE_POOL_REMOTE_KEY}, {@code MAX_POOL_REMOTE_KEY},
96+ * {@code NEW_CONNECTION_THRESHOLD_LOCAL_KEY}, {@code NEW_CONNECTION_THRESHOLD_REMOTE_KEY},
97+ * {@code MAX_REQUESTS_PER_CONNECTION_LOCAL_KEY} and {@code MAX_REQUESTS_PER_CONNECTION_REMOTE_KEY}.
98+ *
99+ * @see #UNSET
100+ */
101+ public static final Map<ProtocolVersion, Map<String, Integer>> DEFAULTS = ImmutableMap.<ProtocolVersion, Map<String, Integer>>of(
102+ ProtocolVersion.V1, ImmutableMap.<String, Integer>builder()
103+ .put(CORE_POOL_LOCAL_KEY, 2)
104+ .put(MAX_POOL_LOCAL_KEY, 8)
105+ .put(CORE_POOL_REMOTE_KEY, 1)
106+ .put(MAX_POOL_REMOTE_KEY, 2)
107+ .put(NEW_CONNECTION_THRESHOLD_LOCAL_KEY, 100)
108+ .put(NEW_CONNECTION_THRESHOLD_REMOTE_KEY, 100)
109+ .put(MAX_REQUESTS_PER_CONNECTION_LOCAL_KEY, 128)
110+ .put(MAX_REQUESTS_PER_CONNECTION_REMOTE_KEY, 128)
79111 .build(),
80112
81113 ProtocolVersion.V3, ImmutableMap.<String, Integer>builder()
82- .put("corePoolLocal", 1)
83- .put("maxPoolLocal", 1)
84- .put("corePoolRemote", 1)
85- .put("maxPoolRemote", 1)
86- .put("newConnectionThresholdLocal", 800)
87- .put("newConnectionThresholdRemote", 200)
88- .put("maxRequestsPerConnectionLocal", 1024)
89- .put("maxRequestsPerConnectionRemote", 256)
114+ .put(CORE_POOL_LOCAL_KEY, 1)
115+ .put(MAX_POOL_LOCAL_KEY, 1)
116+ .put(CORE_POOL_REMOTE_KEY, 1)
117+ .put(MAX_POOL_REMOTE_KEY, 1)
118+ .put(NEW_CONNECTION_THRESHOLD_LOCAL_KEY, 800)
119+ .put(NEW_CONNECTION_THRESHOLD_REMOTE_KEY, 200)
120+ .put(MAX_REQUESTS_PER_CONNECTION_LOCAL_KEY, 1024)
121+ .put(MAX_REQUESTS_PER_CONNECTION_REMOTE_KEY, 256)
90122 .build()
91123 );
92124
93- private static final int DEFAULT_IDLE_TIMEOUT_SECONDS = 120;
94- private static final int DEFAULT_POOL_TIMEOUT_MILLIS = 5000;
95- private static final int DEFAULT_HEARTBEAT_INTERVAL_SECONDS = 30;
125+ /**
126+ * The default value for {@link #getIdleTimeoutSeconds()} ({@value}).
127+ */
128+ public static final int DEFAULT_IDLE_TIMEOUT_SECONDS = 120;
129+
130+ /**
131+ * The default value for {@link #getPoolTimeoutMillis()} ({@value}).
132+ */
133+ public static final int DEFAULT_POOL_TIMEOUT_MILLIS = 5000;
134+
135+ /**
136+ * The default value for {@link #getHeartbeatIntervalSeconds()} ({@value}).
137+ */
138+ public static final int DEFAULT_HEARTBEAT_INTERVAL_SECONDS = 30;
96139
97140 private static final Executor DEFAULT_INITIALIZATION_EXECUTOR = MoreExecutors.sameThreadExecutor();
98141
@@ -450,40 +493,46 @@
450493 return this;
451494 }
452495
453- synchronized void setProtocolVersion(ProtocolVersion protocolVersion) {
454- this.protocolVersion = protocolVersion;
496+ synchronized void setProtocolVersion(ProtocolVersion actualVersion) {
497+ this.protocolVersion = actualVersion;
498+
499+ ProtocolVersion referenceVersion = null;
500+ for (ProtocolVersion key : DEFAULTS.keySet()) {
501+ if (key.compareTo(actualVersion) > 0)
502+ break;
503+ else
504+ referenceVersion = key;
505+ }
506+ assert referenceVersion != null; // will not happen since V1 is a key
455507
456- ProtocolVersion referenceVersion = (protocolVersion.compareTo(ProtocolVersion.V2) <= 0)
457- ? ProtocolVersion.V2
458- : ProtocolVersion.V3;
459508 Map<String, Integer> defaults = DEFAULTS.get(referenceVersion);
460509
461510 if (coreConnections[LOCAL.ordinal()] == UNSET)
462- coreConnections[LOCAL.ordinal()] = defaults.get("corePoolLocal");
511+ coreConnections[LOCAL.ordinal()] = defaults.get(CORE_POOL_LOCAL_KEY);
463512 if (maxConnections[LOCAL.ordinal()] == UNSET)
464- maxConnections[LOCAL.ordinal()] = defaults.get("maxPoolLocal");
513+ maxConnections[LOCAL.ordinal()] = defaults.get(MAX_POOL_LOCAL_KEY);
465514 checkConnectionsPerHostOrder(coreConnections[LOCAL.ordinal()], maxConnections[LOCAL.ordinal()], LOCAL);
466515
467516 if (coreConnections[REMOTE.ordinal()] == UNSET)
468- coreConnections[REMOTE.ordinal()] = defaults.get("corePoolRemote");
517+ coreConnections[REMOTE.ordinal()] = defaults.get(CORE_POOL_REMOTE_KEY);
469518 if (maxConnections[REMOTE.ordinal()] == UNSET)
470- maxConnections[REMOTE.ordinal()] = defaults.get("maxPoolRemote");
519+ maxConnections[REMOTE.ordinal()] = defaults.get(MAX_POOL_REMOTE_KEY);
471520 checkConnectionsPerHostOrder(coreConnections[REMOTE.ordinal()], maxConnections[REMOTE.ordinal()], REMOTE);
472521
473522 if (newConnectionThreshold[LOCAL.ordinal()] == UNSET)
474- newConnectionThreshold[LOCAL.ordinal()] = defaults.get("newConnectionThresholdLocal");
523+ newConnectionThreshold[LOCAL.ordinal()] = defaults.get(NEW_CONNECTION_THRESHOLD_LOCAL_KEY);
475524 checkRequestsPerConnectionRange(newConnectionThreshold[LOCAL.ordinal()], "New connection threshold", LOCAL);
476525
477526 if (newConnectionThreshold[REMOTE.ordinal()] == UNSET)
478- newConnectionThreshold[REMOTE.ordinal()] = defaults.get("newConnectionThresholdRemote");
527+ newConnectionThreshold[REMOTE.ordinal()] = defaults.get(NEW_CONNECTION_THRESHOLD_REMOTE_KEY);
479528 checkRequestsPerConnectionRange(newConnectionThreshold[REMOTE.ordinal()], "New connection threshold", REMOTE);
480529
481530 if (maxRequestsPerConnectionLocal == UNSET)
482- maxRequestsPerConnectionLocal = defaults.get("maxRequestsPerConnectionLocal");
531+ maxRequestsPerConnectionLocal = defaults.get(MAX_REQUESTS_PER_CONNECTION_LOCAL_KEY);
483532 checkRequestsPerConnectionRange(maxRequestsPerConnectionLocal, "Max requests per connection", LOCAL);
484533
485534 if (maxRequestsPerConnectionRemote == UNSET)
486- maxRequestsPerConnectionRemote = defaults.get("maxRequestsPerConnectionRemote");
535+ maxRequestsPerConnectionRemote = defaults.get(MAX_REQUESTS_PER_CONNECTION_REMOTE_KEY);
487536 checkRequestsPerConnectionRange(maxRequestsPerConnectionRemote, "Max requests per connection", REMOTE);
488537 }
489538