-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSkyflow.java
More file actions
192 lines (170 loc) · 8.27 KB
/
Copy pathSkyflow.java
File metadata and controls
192 lines (170 loc) · 8.27 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
package com.skyflow;
import com.skyflow.config.Credentials;
import com.skyflow.config.VaultConfig;
import com.skyflow.enums.LogLevel;
import com.skyflow.errors.ErrorCode;
import com.skyflow.errors.ErrorMessage;
import com.skyflow.errors.SkyflowException;
import com.skyflow.logs.ErrorLogs;
import com.skyflow.logs.InfoLogs;
import com.skyflow.utils.Constants;
import com.skyflow.utils.SdkVersion;
import com.skyflow.utils.Utils;
import com.skyflow.utils.logger.LogUtil;
import com.skyflow.utils.validations.Validations;
import com.skyflow.vault.controller.VaultController;
import java.util.LinkedHashMap;
public final class Skyflow extends BaseSkyflow {
private final SkyflowClientBuilder builder;
private Skyflow(SkyflowClientBuilder builder) {
super(builder);
this.builder = builder;
com.skyflow.utils.logger.LogUtil.printInfoLog(InfoLogs.CLIENT_INITIALIZED.getLog());
}
public static SkyflowClientBuilder builder() {
SdkVersion.setSdkPrefix(Constants.SDK_PREFIX);
return new SkyflowClientBuilder();
}
public VaultConfig getVaultConfig() {
Object[] array = this.builder.vaultConfigMap.values().toArray();
return (VaultConfig) array[0];
}
public VaultController vault() throws SkyflowException {
Object[] array = this.builder.vaultClientsMap.keySet().toArray();
if (array.length < 1) {
LogUtil.printErrorLog(ErrorLogs.VAULT_CONFIG_DOES_NOT_EXIST.getLog());
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.VaultIdNotInConfigList.getMessage());
}
String vaultId = (String) array[0];
VaultController controller = this.builder.vaultClientsMap.get(vaultId);
if (controller == null) {
LogUtil.printErrorLog(ErrorLogs.VAULT_CONFIG_DOES_NOT_EXIST.getLog());
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.VaultIdNotInConfigList.getMessage());
}
return controller;
}
public static final class SkyflowClientBuilder extends BaseSkyflowClientBuilder {
private final LinkedHashMap<String, VaultConfig> vaultConfigMap;
private final LinkedHashMap<String, VaultController> vaultClientsMap;
// Client-wide HTTP config defaults (apply to all vaults unless a vault overrides). null => SDK default.
private Integer timeout;
private Integer connectTimeout;
private Integer readTimeout;
private Integer writeTimeout;
private Integer maxRetries;
private Long initialRetryDelayMillis;
private Long maxRetryDelayMillis;
public SkyflowClientBuilder() {
this.vaultConfigMap = new LinkedHashMap<>();
this.vaultClientsMap = new LinkedHashMap<>();
}
public SkyflowClientBuilder addVaultConfig(VaultConfig vaultConfig) throws SkyflowException {
LogUtil.printInfoLog(InfoLogs.VALIDATING_VAULT_CONFIG.getLog());
Validations.validateVaultConfiguration(vaultConfig);
VaultConfig vaultConfigCopy;
try {
vaultConfigCopy = (VaultConfig) vaultConfig.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
if (!this.vaultClientsMap.isEmpty()) {
LogUtil.printErrorLog(Utils.parameterizedString(
ErrorLogs.VAULT_CONFIG_EXISTS.getLog(), vaultConfigCopy.getVaultId()
));
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(),
ErrorMessage.VaultIdAlreadyInConfigList.getMessage());
} else {
this.vaultConfigMap.put(vaultConfigCopy.getVaultId(), vaultConfigCopy); // add new config in map
VaultController controller = new VaultController(vaultConfigCopy, this.skyflowCredentials); // new controller with new config
controller.setCommonHttpConfig(this.timeout, this.connectTimeout, this.readTimeout,
this.writeTimeout, this.maxRetries, this.initialRetryDelayMillis, this.maxRetryDelayMillis);
this.vaultClientsMap.put(vaultConfigCopy.getVaultId(), controller);
LogUtil.printInfoLog(Utils.parameterizedString(
InfoLogs.VAULT_CONTROLLER_INITIALIZED.getLog(), vaultConfigCopy.getVaultId()));
}
return this;
}
public SkyflowClientBuilder addSkyflowCredentials(Credentials credentials) throws SkyflowException {
Validations.validateCredentials(credentials);
Credentials credentialsCopy;
try {
credentialsCopy = (Credentials) credentials.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
this.skyflowCredentials = credentialsCopy;
for (VaultController vault : this.vaultClientsMap.values()) {
vault.setCommonCredentials(this.skyflowCredentials);
}
return this;
}
/** Client-wide overall call timeout in seconds. Applies to all vaults unless a vault overrides it. */
public SkyflowClientBuilder timeout(int timeout) {
this.timeout = timeout;
propagateHttpConfig();
return this;
}
/**
* Client-wide per-attempt connection-establishment timeout in seconds. Applies to all vaults
* unless a vault overrides it; when unset the underlying HTTP client default (10s) applies.
* The overall {@code timeout} still bounds the whole call, including retries.
*/
public SkyflowClientBuilder connectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
propagateHttpConfig();
return this;
}
/**
* Client-wide per-attempt response-read timeout in seconds. Applies to all vaults unless a
* vault overrides it; when unset the underlying HTTP client default (10s) applies. The overall
* {@code timeout} still bounds the whole call, including retries.
*/
public SkyflowClientBuilder readTimeout(int readTimeout) {
this.readTimeout = readTimeout;
propagateHttpConfig();
return this;
}
/**
* Client-wide per-attempt request-write timeout in seconds. Applies to all vaults unless a
* vault overrides it; when unset the underlying HTTP client default (10s) applies. The overall
* {@code timeout} still bounds the whole call, including retries.
*/
public SkyflowClientBuilder writeTimeout(int writeTimeout) {
this.writeTimeout = writeTimeout;
propagateHttpConfig();
return this;
}
/** Client-wide retry attempt count. Applies to all vaults unless a vault overrides it. */
public SkyflowClientBuilder maxRetries(int maxRetries) {
this.maxRetries = maxRetries;
propagateHttpConfig();
return this;
}
/** Client-wide base retry backoff in milliseconds. Applies to all vaults unless a vault overrides it. */
public SkyflowClientBuilder initialRetryDelayMillis(long initialRetryDelayMillis) {
this.initialRetryDelayMillis = initialRetryDelayMillis;
propagateHttpConfig();
return this;
}
/** Client-wide retry backoff cap in milliseconds. Applies to all vaults unless a vault overrides it. */
public SkyflowClientBuilder maxRetryDelayMillis(long maxRetryDelayMillis) {
this.maxRetryDelayMillis = maxRetryDelayMillis;
propagateHttpConfig();
return this;
}
private void propagateHttpConfig() {
for (VaultController vault : this.vaultClientsMap.values()) {
vault.setCommonHttpConfig(this.timeout, this.connectTimeout, this.readTimeout,
this.writeTimeout, this.maxRetries, this.initialRetryDelayMillis, this.maxRetryDelayMillis);
}
}
@Override
public SkyflowClientBuilder setLogLevel(LogLevel logLevel) {
super.setLogLevel(logLevel);
return this;
}
public Skyflow build() {
return new Skyflow(this);
}
}
}