Skip to content

connect only to primary host for load balancer scenario #999 #1001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 25, 2018
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@
import com.marklogic.client.pojo.PojoRepository;
import com.marklogic.client.semantics.GraphManager;
import com.marklogic.client.semantics.SPARQLQueryManager;
import com.marklogic.client.DatabaseClientFactory.Authentication;
import com.marklogic.client.DatabaseClientFactory.SSLHostnameVerifier;
import com.marklogic.client.DatabaseClientFactory.SecurityContext;

import javax.net.ssl.SSLContext;

/**
* A Database Client instantiates document and query managers and other objects
* with shared access to a database.
*/
public interface DatabaseClient {
/**
* Identifies whether the client connects directly to MarkLogic (the default) or
* by means of a gateway such as a load balancer.
*/
enum ConnectionType {DIRECT, GATEWAY}

/**
* Starts a transaction. You can pass the transaction to the read(), write(), or delete() methods
* of a document manager or the search() method of a query manager to perform operations within a
Expand Down Expand Up @@ -100,8 +102,14 @@ public interface DatabaseClient {
XMLDocumentManager newXMLDocumentManager();

/**
* Creates a manager for long-running asynchronous write or query jobs. Don't forget
* to call dataMovementManager.release() when you're done with it.
* Creates a manager for long-running asynchronous write or query jobs.
* When the primary database client has the default ConnectionType.DIRECT
* connection type, the DataMovementManager creates a new connection
* for each host that has forests for the database. When the primary
* database client has the ConnectionType.GATEWAY connection type
* (for instance, when connecting to a load balancer), the DataMovementManager
* uses the primary database client for all communication.
* Don't forget to call dataMovementManager.release() when you're done with it.
* @return a manager supporting long-running asynchronous write or query jobs
*/
DataMovementManager newDataMovementManager();
Expand Down Expand Up @@ -212,6 +220,12 @@ public interface DatabaseClient {
*/
ServerEvaluationCall newServerEval();

/**
* How the client connects to MarkLogic.
* @return the connection type
*/
ConnectionType getConnectionType();

String getHost();

int getPort();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,25 @@ static public DatabaseClient newClient(String host, int port, String database) {
* @return a new client for making database requests
*/
static public DatabaseClient newClient(String host, int port, SecurityContext securityContext) {
return newClient(host, port, null, securityContext);
return newClient(host, port, null, securityContext, null);
}

/**
* Creates a client to access the database by means of a REST server.
*
* @param host the host with the REST server
* @param port the port for the REST server
* @param securityContext the security context created depending upon the
* authentication type - BasicAuthContext, DigestAuthContext or KerberosAuthContext
* and communication channel type (SSL)
* @param connectionType whether the client connects directly to the MarkLogic host
* or using a gateway such as a load balancer
* @return a new client for making database requests
*/
static public DatabaseClient newClient(String host, int port, SecurityContext securityContext,
DatabaseClient.ConnectionType connectionType)
{
return newClient(host, port, null, securityContext, connectionType);
}

/**
Expand All @@ -703,6 +721,27 @@ static public DatabaseClient newClient(String host, int port, SecurityContext se
* @return a new client for making database requests
*/
static public DatabaseClient newClient(String host, int port, String database, SecurityContext securityContext) {
return newClient(host, port, null, securityContext, null);
}

/**
* Creates a client to access the database by means of a REST server.
*
* @param host the host with the REST server
* @param port the port for the REST server
* @param database the database to access (default: configured database for
* the REST server)
* @param securityContext the security context created depending upon the
* authentication type - BasicAuthContext, DigestAuthContext or KerberosAuthContext
* and communication channel type (SSL)
* @param connectionType whether the client connects directly to the MarkLogic host
* or using a gateway such as a load balancer
* @return a new client for making database requests
*/
static public DatabaseClient newClient(String host, int port, String database,
SecurityContext securityContext,
DatabaseClient.ConnectionType connectionType)
{
String user = null;
String password = null;
Authentication type = null;
Expand Down Expand Up @@ -783,7 +822,9 @@ static public DatabaseClient newClient(String host, int port, String database, S
}
}

DatabaseClientImpl client = new DatabaseClientImpl(services, host, port, database, securityContext);
DatabaseClientImpl client = new DatabaseClientImpl(
services, host, port, database, securityContext, connectionType
);
client.setHandleRegistry(getHandleRegistry().copy());
return client;
}
Expand Down Expand Up @@ -816,7 +857,7 @@ static private SecurityContext makeSecurityContext(String user, String password,
*/
@Deprecated
static public DatabaseClient newClient(String host, int port, String user, String password, Authentication type) {
return newClient(host, port, null, makeSecurityContext(user, password, type, null, null));
return newClient(host, port, null, makeSecurityContext(user, password, type, null, null), null);
}
/**
* Creates a client to access the database by means of a REST server.
Expand All @@ -832,7 +873,7 @@ static public DatabaseClient newClient(String host, int port, String user, Strin
*/
@Deprecated
static public DatabaseClient newClient(String host, int port, String database, String user, String password, Authentication type) {
return newClient(host, port, database, makeSecurityContext(user, password, type, null, null));
return newClient(host, port, database, makeSecurityContext(user, password, type, null, null), null);
}
/**
* Creates a client to access the database by means of a REST server.
Expand All @@ -848,7 +889,7 @@ static public DatabaseClient newClient(String host, int port, String database, S
*/
@Deprecated
static public DatabaseClient newClient(String host, int port, String user, String password, Authentication type, SSLContext context) {
return newClient(host, port, null, makeSecurityContext(user, password, type, context, SSLHostnameVerifier.COMMON));
return newClient(host, port, null, makeSecurityContext(user, password, type, context, SSLHostnameVerifier.COMMON), null);
}
/**
* Creates a client to access the database by means of a REST server.
Expand All @@ -865,7 +906,7 @@ static public DatabaseClient newClient(String host, int port, String user, Strin
*/
@Deprecated
static public DatabaseClient newClient(String host, int port, String database, String user, String password, Authentication type, SSLContext context) {
return newClient(host, port, database, makeSecurityContext(user, password, type, context, SSLHostnameVerifier.COMMON));
return newClient(host, port, database, makeSecurityContext(user, password, type, context, SSLHostnameVerifier.COMMON), null);
}
/**
* Creates a client to access the database by means of a REST server.
Expand All @@ -882,7 +923,7 @@ static public DatabaseClient newClient(String host, int port, String database, S
*/
@Deprecated
static public DatabaseClient newClient(String host, int port, String user, String password, Authentication type, SSLContext context, SSLHostnameVerifier verifier) {
return newClient(host, port, null, makeSecurityContext(user, password, type, context, verifier));
return newClient(host, port, null, makeSecurityContext(user, password, type, context, verifier), null);
}
/**
* Creates a client to access the database by means of a REST server.
Expand All @@ -900,7 +941,7 @@ static public DatabaseClient newClient(String host, int port, String user, Strin
*/
@Deprecated
static public DatabaseClient newClient(String host, int port, String database, String user, String password, Authentication type, SSLContext context, SSLHostnameVerifier verifier) {
return newClient(host, port, database, makeSecurityContext(user, password, type, context, verifier));
return newClient(host, port, database, makeSecurityContext(user, password, type, context, verifier), null);
}

/**
Expand Down Expand Up @@ -978,6 +1019,7 @@ static public class Bean implements Serializable {
private Authentication authentication;
private String externalName;
private SecurityContext securityContext;
private DatabaseClient.ConnectionType connectionType;
private HandleFactoryRegistry handleRegistry =
HandleFactoryRegistryImpl.newDefault();

Expand Down Expand Up @@ -1189,6 +1231,24 @@ public SecurityContext getSecurityContext() {
public void setSecurityContext(SecurityContext securityContext) {
this.securityContext = securityContext;
}
/**
* Identifies whether the client connects directly with a MarkLogic host
* or by means of a gateway such as a load balancer.
* @return the connection type
*/
public DatabaseClient.ConnectionType getConnectionType() {
return connectionType;
}
/**
* Specify whether the client connects directly with a MarkLogic host
* or by means of a gateway such as a load balancer.
* @param connectionType the connection type
*/
public void setConnectionType(DatabaseClient.ConnectionType connectionType) {
this.connectionType = connectionType;
}


/**
* Returns the registry for associating
* IO representation classes with handle factories.
Expand Down Expand Up @@ -1222,8 +1282,10 @@ public void registerDefaultHandles() {
* @return a new client for making database requests
*/
public DatabaseClient newClient() {
DatabaseClientImpl client = (DatabaseClientImpl) DatabaseClientFactory.newClient(host, port, database,
makeSecurityContext(user, password, authentication, context, verifier));
DatabaseClientImpl client = (DatabaseClientImpl) DatabaseClientFactory.newClient(
host, port, database,
makeSecurityContext(user, password, authentication, context, verifier),
connectionType);
client.setHandleRegistry(getHandleRegistry().copy());
return client;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2012-2018 MarkLogic Corporation
*
* 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 com.marklogic.client;

import com.marklogic.client.impl.FailedRequest;

/**
* A FailedRetryException is used to capture and report when retry
* of the request timed out or failed in some other way.
*/
@SuppressWarnings("serial")
public class FailedRetryException extends FailedRequestException {

public FailedRetryException(String message) {
super(message);
}

public FailedRetryException(String localMessage, Throwable cause) {
super(localMessage, cause);
}

public FailedRetryException(String localMessage, FailedRequest failedRequest) {
super(localMessage, failedRequest);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
package com.marklogic.client.datamovement;

import com.marklogic.client.DatabaseClient;
import com.marklogic.client.query.QueryDefinition;
import com.marklogic.client.query.StringQueryDefinition;
import com.marklogic.client.query.StructuredQueryDefinition;
import com.marklogic.client.query.RawCombinedQueryDefinition;
import com.marklogic.client.query.RawStructuredQueryDefinition;
import com.marklogic.client.datamovement.impl.DataMovementManagerImpl;

import java.util.Iterator;

Expand Down Expand Up @@ -184,4 +182,12 @@ public interface DataMovementManager {
* @return the latest ForestConfiguration from the server
*/
public ForestConfiguration readForestConfig();

/**
* Identify whether the DataMovementManager connects directly to each MarkLogic host
* with a forest for the database or whether the DataMovementManager uses a gateway
* such as a load balancer to communicate with the MarkLogic hosts.
* @return the connection type
*/
public DatabaseClient.ConnectionType getConnectionType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@
package com.marklogic.client.datamovement;

import com.marklogic.client.datamovement.Forest.HostType;
import com.marklogic.client.datamovement.impl.ForestConfigurationImpl;
import com.marklogic.client.datamovement.impl.ForestImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
Expand Down
Loading