17
17
package com .netflix .spinnaker .kork .astyanax ;
18
18
19
19
import com .google .common .collect .ImmutableMap ;
20
+ import com .google .common .util .concurrent .ThreadFactoryBuilder ;
20
21
import com .netflix .astyanax .AstyanaxConfiguration ;
21
22
import com .netflix .astyanax .Keyspace ;
22
23
import com .netflix .astyanax .connectionpool .ConnectionPoolConfiguration ;
23
- import com .netflix .astyanax .connectionpool .ConnectionPoolMonitor ;
24
24
import com .netflix .astyanax .connectionpool .NodeDiscoveryType ;
25
25
import com .netflix .astyanax .connectionpool .exceptions .ConnectionException ;
26
26
import com .netflix .astyanax .connectionpool .impl .ConnectionPoolConfigurationImpl ;
27
27
import com .netflix .astyanax .connectionpool .impl .ConnectionPoolType ;
28
- import com .netflix .astyanax .connectionpool .impl .CountingConnectionPoolMonitor ;
29
28
import com .netflix .astyanax .impl .AstyanaxConfigurationImpl ;
29
+ import com .netflix .astyanax .model .ConsistencyLevel ;
30
30
import com .netflix .astyanax .test .EmbeddedCassandra ;
31
+ import com .netflix .discovery .DiscoveryClient ;
32
+ import com .netflix .spectator .api .Registry ;
31
33
import org .slf4j .Logger ;
32
34
import org .slf4j .LoggerFactory ;
35
+ import org .springframework .beans .factory .annotation .Qualifier ;
33
36
import org .springframework .beans .factory .annotation .Value ;
34
- import org .springframework .boot .autoconfigure .condition .ConditionalOnBean ;
35
- import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
36
- import org .springframework .boot .autoconfigure .condition .ConditionalOnExpression ;
37
- import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingClass ;
37
+ import org .springframework .boot .autoconfigure .condition .*;
38
+ import org .springframework .boot .context .properties .ConfigurationProperties ;
39
+ import org .springframework .boot .context .properties .EnableConfigurationProperties ;
38
40
import org .springframework .context .annotation .Bean ;
39
41
import org .springframework .context .annotation .Configuration ;
40
42
44
46
import java .io .IOException ;
45
47
import java .net .Socket ;
46
48
import java .util .Map ;
47
- import java .util .concurrent .Callable ;
48
- import java .util .concurrent .Executors ;
49
- import java .util .concurrent .Future ;
50
- import java .util .concurrent .TimeUnit ;
49
+ import java .util .concurrent .*;
51
50
52
51
@ Configuration
53
- @ ConditionalOnMissingClass ( name = { "com.netflix.cassandra.NFAstyanaxManager" })
52
+ @ EnableConfigurationProperties
54
53
@ ConditionalOnClass (AstyanaxConfiguration .class )
55
54
public class AstyanaxComponents {
56
55
56
+ @ Value ("${cassandra.host:127.0.0.1}" )
57
+ String seeds ;
58
+
59
+ @ Bean
60
+ public ExecutorService cassandraAsyncExecutor (@ Value ("${cassandra.asyncExecutorPoolSize:5}" ) int asyncPoolSize ) {
61
+ return Executors .newFixedThreadPool (asyncPoolSize ,
62
+ new ThreadFactoryBuilder ().setDaemon (true )
63
+ .setNameFormat ("AstyanaxAsync-%d" )
64
+ .build ());
65
+ }
66
+
57
67
@ Bean
58
- public AstyanaxConfiguration astyanaxConfiguration () {
68
+ @ ConditionalOnMissingBean (AstyanaxConfiguration .class )
69
+ @ ConfigurationProperties ("cassandra" )
70
+ public AstyanaxConfiguration astyanaxConfiguration (@ Qualifier ("cassandraAsyncExecutor" ) ExecutorService cassandraAsyncExecutor ) {
59
71
return new AstyanaxConfigurationImpl ()
72
+ .setDefaultReadConsistencyLevel (ConsistencyLevel .CL_LOCAL_QUORUM )
73
+ .setDefaultWriteConsistencyLevel (ConsistencyLevel .CL_LOCAL_QUORUM )
60
74
.setDiscoveryType (NodeDiscoveryType .RING_DESCRIBE )
61
75
.setConnectionPoolType (ConnectionPoolType .TOKEN_AWARE )
62
76
.setCqlVersion ("3.0.0" )
63
- .setTargetCassandraVersion ("2.0" );
77
+ .setTargetCassandraVersion ("2.0" )
78
+ .setAsyncExecutor (cassandraAsyncExecutor );
64
79
}
65
80
81
+
82
+
66
83
@ Bean
67
- public ConnectionPoolMonitor connectionPoolMonitor () {
68
- return new CountingConnectionPoolMonitor ();
84
+ @ ConditionalOnBean (DiscoveryClient .class )
85
+ @ ConditionalOnProperty ("cassandra.eureka.enabled" )
86
+ public ClusterHostSupplierFactory eurekaHostSupplier (DiscoveryClient discoveryClient ) {
87
+ return EurekaHostSupplier .factory (discoveryClient );
69
88
}
70
89
71
90
@ Bean
72
- public ConnectionPoolConfiguration connectionPoolConfiguration (@ Value ("${cassandra.port:9160}" ) int port , @ Value ("${cassandra.host:127.0.0.1}" ) String seeds , @ Value ("${cassandra.maxConns:3}" ) int maxConns ) {
73
- return new ConnectionPoolConfigurationImpl ("cpConfig" ).setPort (port ).setSeeds (seeds ).setMaxConns (maxConns );
91
+ @ ConditionalOnMissingBean (ClusterHostSupplierFactory .class )
92
+ public ClusterHostSupplierFactory clusterHostSupplierFactory () {
93
+ return ClusterHostSupplierFactory .nullSupplierFactory ();
94
+ }
95
+
96
+ @ Bean
97
+ @ ConditionalOnBean (Registry .class )
98
+ public KeyspaceConnectionPoolMonitorFactory spectatorConnectionPoolMonitor (Registry registry ) {
99
+ return SpectatorConnectionPoolMonitor .factory (registry );
100
+ }
101
+
102
+ @ Bean
103
+ @ ConditionalOnMissingBean (KeyspaceConnectionPoolMonitorFactory .class )
104
+ public KeyspaceConnectionPoolMonitorFactory countingConnectionPoolMonitor () {
105
+ return KeyspaceConnectionPoolMonitorFactory .defaultFactory ();
106
+ }
107
+
108
+ @ Bean
109
+ @ ConfigurationProperties ("cassandra" )
110
+ public ConnectionPoolConfiguration connectionPoolConfiguration () {
111
+ return new ConnectionPoolConfigurationImpl ("cpConfig" ).setSeeds (seeds );
74
112
}
75
113
76
114
@ Bean
77
115
public AstyanaxKeyspaceFactory keyspaceFactory (AstyanaxConfiguration config ,
78
116
ConnectionPoolConfiguration poolConfig ,
79
- ConnectionPoolMonitor poolMonitor ) {
80
- return new DefaultAstyanaxKeyspaceFactory (config , poolConfig , poolMonitor );
117
+ KeyspaceConnectionPoolMonitorFactory connectionPoolMonitorFactory ,
118
+ ClusterHostSupplierFactory clusterHostSupplierFactory ,
119
+ KeyspaceInitializer keyspaceInitializer ) {
120
+ return new DefaultAstyanaxKeyspaceFactory (config , poolConfig , connectionPoolMonitorFactory , clusterHostSupplierFactory , keyspaceInitializer );
81
121
}
82
122
83
123
@ ConditionalOnExpression ("${cassandra.embedded:true} and '${cassandra.host:127.0.0.1}' == '127.0.0.1'" )
124
+ @ Bean
84
125
@ ConditionalOnBean (Keyspace .class )
126
+ public KeyspaceInitializer embeddedCassandra (@ Value ("${cassandra.port:9160}" ) int port ,
127
+ @ Value ("${cassandra.storagePort:7000}" ) int storagePort ,
128
+ @ Value ("${cassandra.host:127.0.0.1}" ) String host ) {
129
+ return new EmbeddedCassandraRunner (port , storagePort , host );
130
+ }
131
+
132
+ @ ConditionalOnMissingBean (KeyspaceInitializer .class )
85
133
@ Bean
86
- public EmbeddedCassandraRunner embeddedCassandra (Keyspace keyspace , @ Value ("${cassandra.port:9160}" ) int port ,
87
- @ Value ("${cassandra.storagePort:7000}" ) int storagePort ,
88
- @ Value ("${cassandra.host:127.0.0.1}" ) String host ) {
89
- return new EmbeddedCassandraRunner (keyspace , port , storagePort , host );
134
+ public KeyspaceInitializer noopKeyspaceInitializer () {
135
+ return new KeyspaceInitializer () {
136
+ @ Override
137
+ public void initKeyspace (Keyspace keyspace ) throws ConnectionException {
138
+ //noop
139
+ }
140
+ };
90
141
}
91
142
92
- public static class EmbeddedCassandraRunner {
143
+ public static class EmbeddedCassandraRunner implements KeyspaceInitializer {
93
144
private static final Logger log = LoggerFactory .getLogger (EmbeddedCassandraRunner .class );
94
145
95
- private final Keyspace keyspace ;
96
146
private final int port ;
97
147
private final int storagePort ;
98
148
private final String host ;
99
149
private EmbeddedCassandra embeddedCassandra ;
100
150
101
- public EmbeddedCassandraRunner (Keyspace keyspace , int port , int storagePort , String host ) {
102
- this .keyspace = keyspace ;
151
+ public EmbeddedCassandraRunner (int port , int storagePort , String host ) {
103
152
this .port = port ;
104
153
this .storagePort = storagePort ;
105
154
this .host = host ;
@@ -126,14 +175,18 @@ public Object call() throws Exception {
126
175
});
127
176
waitForCassandraFuture .get (60 , TimeUnit .SECONDS );
128
177
log .info ("Embedded cassandra started." );
178
+ }
179
+
180
+ @ Override
181
+ public void initKeyspace (Keyspace keyspace ) throws ConnectionException {
129
182
try {
130
183
keyspace .describeKeyspace ();
131
184
} catch (ConnectionException e ) {
132
185
Map <String , Object > options = ImmutableMap .<String , Object >builder ()
133
- .put ("name" , keyspace .getKeyspaceName ())
134
- .put ("strategy_class" , "SimpleStrategy" )
135
- .put ("strategy_options" , ImmutableMap .of ("replication_factor" , "1" ))
136
- .build ();
186
+ .put ("name" , keyspace .getKeyspaceName ())
187
+ .put ("strategy_class" , "SimpleStrategy" )
188
+ .put ("strategy_options" , ImmutableMap .of ("replication_factor" , "1" ))
189
+ .build ();
137
190
keyspace .createKeyspace (options );
138
191
}
139
192
}
0 commit comments