Skip to content

Commit 7aedfb1

Browse files
rahulvudutalaMarkLogic Builder
authored andcommitted
DHFPROD-10578: Changing waiting strategy for ml cloud gateway restart
1 parent ee2ef2e commit 7aedfb1

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

marklogic-data-hub-api/src/main/java/com/marklogic/hub/HubClientConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ public DatabaseClient newStagingClient(String dbName) {
208208
config.setCloudApiKey(cloudApiKey);
209209
config.setBasePath(stagingBasePath);
210210
config.setPort(443);
211+
config.setConnectionType(DatabaseClient.ConnectionType.GATEWAY);
211212
}
212213
if (isHostLoadBalancer) {
213214
config.setConnectionType(DatabaseClient.ConnectionType.GATEWAY);
@@ -232,6 +233,7 @@ public DatabaseClient newFinalClient(String dbName) {
232233
config.setCloudApiKey(cloudApiKey);
233234
config.setBasePath(finalBasePath);
234235
config.setPort(443);
236+
config.setConnectionType(DatabaseClient.ConnectionType.GATEWAY);
235237
}
236238
if (isHostLoadBalancer) {
237239
config.setConnectionType(DatabaseClient.ConnectionType.GATEWAY);
@@ -254,6 +256,7 @@ public DatabaseClient newJobDbClient() {
254256
config.setCloudApiKey(cloudApiKey);
255257
config.setBasePath(jobBasePath);
256258
config.setPort(443);
259+
config.setConnectionType(DatabaseClient.ConnectionType.GATEWAY);
257260
}
258261
if (isHostLoadBalancer) {
259262
config.setConnectionType(DatabaseClient.ConnectionType.GATEWAY);

marklogic-data-hub/src/main/java/com/marklogic/hub/deploy/commands/ConfigureAppServerBasePaths.java

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import com.marklogic.appdeployer.command.CommandContext;
88
import com.marklogic.appdeployer.command.SortOrderConstants;
99
import com.marklogic.client.DatabaseClient;
10+
import com.marklogic.hub.HubClient;
1011
import com.marklogic.hub.HubConfig;
12+
import com.marklogic.hub.impl.HubClientImpl;
1113
import com.marklogic.hub.impl.HubConfigImpl;
1214
import org.apache.commons.io.IOUtils;
1315
import org.apache.commons.lang3.StringUtils;
@@ -33,10 +35,12 @@
3335
public class ConfigureAppServerBasePaths extends AbstractCommand {
3436

3537
private final HubConfigImpl hubConfig;
38+
private final HubClientImpl hubClient;
3639
private ObjectMapper mapper = new ObjectMapper();
3740

38-
public ConfigureAppServerBasePaths(HubConfig hubConfig) {
41+
public ConfigureAppServerBasePaths(HubConfig hubConfig, HubClient hubClient) {
3942
this.hubConfig = (HubConfigImpl) hubConfig;
43+
this.hubClient = (HubClientImpl) hubClient;
4044
setExecuteSortOrder(SortOrderConstants.DEPLOY_OTHER_SERVERS + 1);
4145
}
4246

@@ -53,9 +57,10 @@ private void updateAppServersBasePaths() {
5357
try (CloseableHttpClient httpClient = clientBuilder.build()) {
5458
String url = "https://" + hubConfig.getHost() + "/api/service/dataHubEndpoints";
5559
HttpPost postRequest = new HttpPost(url);
56-
StringEntity entity = new StringEntity(getBastPathConfig());
60+
StringEntity entity = new StringEntity(getBasePathConfig());
5761
postRequest.setHeader("Content-Type", "application/json");
58-
postRequest.setHeader("Authorization", "bearer " + getAccessToken());
62+
String accessToken = getAccessToken();
63+
postRequest.setHeader("Authorization", "bearer " + accessToken);
5964
postRequest.setEntity(entity);
6065
httpClient.execute(postRequest);
6166
} catch (IOException e) {
@@ -65,12 +70,13 @@ private void updateAppServersBasePaths() {
6570

6671
private String getAccessToken() {
6772
String tokenRequestURL = "https://".concat(hubConfig.getHost()).concat("/token");
73+
logger.info("tokenRequestURL: " + tokenRequestURL);
6874
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
6975
try (CloseableHttpClient httpClient = clientBuilder.build()) {
7076
{
7177
List<NameValuePair> postParams = new ArrayList<>();
7278
postParams.add(new BasicNameValuePair("grant_type", "apikey"));
73-
postParams.add(new BasicNameValuePair("key", hubConfig.getCloudApiKey()));
79+
postParams.add(new BasicNameValuePair("key", hubConfig.getCloudApiKey().trim()));
7480

7581
HttpPost postRequest = new HttpPost(tokenRequestURL);
7682
postRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
@@ -88,7 +94,7 @@ private String getAccessToken() {
8894
}
8995
}
9096

91-
private String getBastPathConfig() {
97+
private String getBasePathConfig() {
9298
ObjectNode baseConfig = mapper.createObjectNode();
9399
ObjectNode appServers = mapper.createObjectNode();
94100
ObjectNode jobServer = mapper.createObjectNode();
@@ -105,11 +111,11 @@ private String getBastPathConfig() {
105111
String jobBasePath = jobBasePathArray[jobBasePathArray.length - 1];
106112

107113
baseConfig.put("adminPath", hubConfig.getAdminConfig().getBasePath());
108-
baseConfig.put("appServers", appServers);
114+
baseConfig.putIfAbsent("appServers", appServers);
109115

110-
appServers.put("staging", stagingServer);
111-
appServers.put("final", finalServer);
112-
appServers.put("jobs", jobServer);
116+
appServers.putIfAbsent("staging", stagingServer);
117+
appServers.putIfAbsent("final", finalServer);
118+
appServers.putIfAbsent("jobs", jobServer);
113119

114120
stagingServer.put("name", hubConfig.getStagingDbName());
115121
stagingServer.put("path", stagingBasePath.concat("/"));
@@ -124,19 +130,29 @@ private String getBastPathConfig() {
124130
}
125131

126132
private void waitForGateWayToRestart() {
127-
DatabaseClient databaseClient = hubConfig.newStagingClient();
133+
DatabaseClient databaseClient = hubClient.getStagingClient();
128134
int maxTimeToWaitInMs = 90000;
129135
int maxRetries = 15;
130136
int sleepTime = maxTimeToWaitInMs/maxRetries;
131-
while(!databaseClient.checkConnection().isConnected() && maxRetries > 0) {
132-
logger.info("Checking gateway status: " + databaseClient.checkConnection().isConnected());
137+
138+
while(maxRetries > 0) {
133139
try {
134-
Thread.sleep(sleepTime);
135-
} catch (InterruptedException e) {
136-
Thread.currentThread().interrupt();
140+
if(databaseClient.checkConnection().isConnected()) {
141+
logger.info("Checking gateway status: " + databaseClient.checkConnection().isConnected());
142+
break;
143+
} else {
144+
logger.info("Checking gateway status: " + databaseClient.checkConnection().isConnected());
145+
Thread.sleep(sleepTime);
146+
maxRetries --;
147+
}
148+
} catch (Exception e) {
149+
if(e instanceof InterruptedException) {
150+
Thread.currentThread().interrupt();
151+
}
152+
logger.info(e.getMessage());
153+
logger.info("waitForGateWayToRestart catch block");
154+
maxRetries --;
137155
}
138-
maxRetries --;
139156
}
140-
logger.info("Checking gateway status: " + databaseClient.checkConnection().isConnected());
141157
}
142158
}

marklogic-data-hub/src/main/java/com/marklogic/hub/impl/DataHubImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,8 @@ private void updateServerCommandList(Map<String, List<Command>> commandMap) {
708708
}
709709
newCommands.add(c);
710710
}
711-
newCommands.add(new ConfigureAppServerBasePaths(hubConfig));
711+
hubClient = hubClient == null ? hubConfig.newHubClient() : hubClient;
712+
newCommands.add(new ConfigureAppServerBasePaths(hubConfig, hubClient));
712713
commandMap.put(key, newCommands);
713714
}
714715

0 commit comments

Comments
 (0)