forked from Restream/reindexer-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReindexerConfiguration.java
More file actions
224 lines (195 loc) · 8.02 KB
/
ReindexerConfiguration.java
File metadata and controls
224 lines (195 loc) · 8.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Copyright 2020 Restream
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ru.rt.restream.reindexer;
import ru.rt.restream.reindexer.binding.Binding;
import ru.rt.restream.reindexer.binding.builtin.Builtin;
import ru.rt.restream.reindexer.binding.builtin.server.BuiltinServer;
import ru.rt.restream.reindexer.binding.cproto.Cproto;
import ru.rt.restream.reindexer.binding.cproto.DataSourceConfiguration;
import ru.rt.restream.reindexer.binding.cproto.DataSourceFactory;
import ru.rt.restream.reindexer.binding.cproto.DataSourceFactoryStrategy;
import ru.rt.restream.reindexer.convert.FieldConverterRegistry;
import ru.rt.restream.reindexer.convert.FieldConverterRegistryFactory;
import ru.rt.restream.reindexer.exceptions.UnimplementedException;
import java.net.URI;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import javax.net.ssl.SSLSocketFactory;
/**
* Represents approach for bootstrapping Reindexer.
*/
public final class ReindexerConfiguration {
private static final int DEFAULT_CONNECTION_POOL_SIZE = 8;
private final List<String> urls = new ArrayList<>();
private boolean allowUnlistedDataSource = false;
private DataSourceFactory dataSourceFactory = DataSourceFactoryStrategy.NEXT;
private int connectionPoolSize = DEFAULT_CONNECTION_POOL_SIZE;
private Duration requestTimeout = Duration.ofSeconds(60L);
private Duration serverStartupTimeout = Duration.ofMinutes(3L);
private String serverConfigFile = "default-builtin-server-config.yml";
private SSLSocketFactory sslSocketFactory;
private ReindexerConfiguration() {
}
public static ReindexerConfiguration builder() {
return new ReindexerConfiguration();
}
/**
* Configure reindexer database url.
*
* @param url a database url of the form protocol://host:port/database_name
* @return the {@link ReindexerConfiguration} for further customizations
*/
public ReindexerConfiguration url(String url) {
urls.add(url);
return this;
}
/**
* Configure reindexer database urls.
*
* @param urls a list of database url of the form protocol://host:port/database_name
* @return the {@link ReindexerConfiguration} for further customizations
*/
public ReindexerConfiguration urls(List<String> urls) {
this.urls.addAll(Objects.requireNonNull(urls));
return this;
}
/**
* Allows usage of the database urls from #replicationstats which are not in the list of urls.
*
* @param allowUnlistedDataSource enable permission to use unlisted urls
* @return the {@link ReindexerConfiguration} for further customizations
*/
public ReindexerConfiguration allowUnlistedDataSource(boolean allowUnlistedDataSource) {
this.allowUnlistedDataSource = allowUnlistedDataSource;
return this;
}
/**
* Configure a {@link DataSourceFactory}. Defaults to {@link DataSourceFactoryStrategy#NEXT}.
*
* @param dataSourceFactory the {@link DataSourceFactory} to use
* @return the {@link ReindexerConfiguration} for further customizations
*/
public ReindexerConfiguration dataSourceFactory(DataSourceFactory dataSourceFactory) {
this.dataSourceFactory = Objects.requireNonNull(dataSourceFactory, "dataSourceFactory cannot be null");
return this;
}
/**
* Allows customizing a {@link FieldConverterRegistry}.
*
* @param customizer the {@link FieldConverterRegistry} customizer.
* @return the {@link ReindexerConfiguration} for further customizations
*/
public ReindexerConfiguration fieldConverterRegistry(Consumer<FieldConverterRegistry> customizer) {
customizer.accept(FieldConverterRegistryFactory.INSTANCE);
return this;
}
/**
* Configure reindexer connection pool size. Defaults to 8.
*
* @param connectionPoolSize the connection pool size
* @return the {@link ReindexerConfiguration} for further customizations
*/
public ReindexerConfiguration connectionPoolSize(int connectionPoolSize) {
this.connectionPoolSize = connectionPoolSize;
return this;
}
/**
* Configure reindexer request timeout. Defaults to 60 seconds.
*
* @param requestTimeout the request timeout
* @return the {@link ReindexerConfiguration} for further customizations
*/
public ReindexerConfiguration requestTimeout(Duration requestTimeout) {
this.requestTimeout = requestTimeout;
return this;
}
/**
* Configure reindexer server startup timeout. Defaults to 3 minutes.
*
* @param serverStartupTimeout the server startup timeout
* @return the {@link ReindexerConfiguration} for further customizations
*/
public ReindexerConfiguration serverStartupTimeout(Duration serverStartupTimeout) {
this.serverStartupTimeout = serverStartupTimeout;
return this;
}
/**
* Configure reindexer server config file. Defaults to "default-builtin-server-config.yml".
*
* @param serverConfigFile the server config file
* @return the {@link ReindexerConfiguration} for further customizations
*/
public ReindexerConfiguration serverConfigFile(String serverConfigFile) {
this.serverConfigFile = serverConfigFile;
return this;
}
/**
* Configure an {@link SSLSocketFactory}.
*
* @param sslSocketFactory the {@link SSLSocketFactory} to use
* @return the {@link ReindexerConfiguration} for further customizations
*/
public ReindexerConfiguration sslSocketFactory(SSLSocketFactory sslSocketFactory) {
this.sslSocketFactory = sslSocketFactory;
return this;
}
/**
* Build and return reindexer connector instance.
*
* @return configured reindexer connector instance
*/
public Reindexer getReindexer() {
if (urls.isEmpty()) {
throw new IllegalStateException("Url is not configured");
}
String protocol = null;
List<URI> uris = new ArrayList<>();
for (String url : urls) {
URI uri = URI.create(url);
if (protocol == null) {
protocol = uri.getScheme();
} else if (!protocol.equals(uri.getScheme())) {
throw new IllegalArgumentException("Protocol must be the same for all DSNs");
}
uris.add(uri);
}
return new Reindexer(getBinding(protocol, uris));
}
private Binding getBinding(String protocol, List<URI> uris) {
switch (protocol) {
case "cprotos":
if (sslSocketFactory == null) {
sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
}
case "cproto":
DataSourceConfiguration dataSourceConfig = DataSourceConfiguration.builder()
.urls(urls)
.allowUnlistedDataSource(allowUnlistedDataSource)
.sslSocketFactory(sslSocketFactory)
.build();
return new Cproto(dataSourceFactory, dataSourceConfig, connectionPoolSize, requestTimeout);
case "builtin":
return new Builtin(uris.get(0), requestTimeout);
case "builtinserver":
return new BuiltinServer(uris.get(0), serverConfigFile, serverStartupTimeout, requestTimeout);
default:
throw new UnimplementedException("Protocol: '" + protocol + "' is not supported");
}
}
}