-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatic conversion of query/ingestion urls in ingest clients (#268)
- Loading branch information
Showing
8 changed files
with
145 additions
and
221 deletions.
There are no files selected for viewing
81 changes: 17 additions & 64 deletions
81
ingest/src/main/java/com/microsoft/azure/kusto/ingest/IngestClientBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,31 @@ | ||
package com.microsoft.azure.kusto.ingest; | ||
|
||
import com.microsoft.azure.kusto.ingest.exceptions.IngestionClientException; | ||
import com.microsoft.azure.kusto.ingest.source.CompressionType; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.http.client.utils.URIBuilder; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
import java.net.URISyntaxException; | ||
|
||
public abstract class IngestClientBase { | ||
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); | ||
static final String INGEST_PREFIX = "ingest-"; | ||
static final String PROTOCOL_SUFFIX = "://"; | ||
|
||
String connectionDataSource; | ||
private String endpointServiceType; | ||
private String suggestedEndpointUri; | ||
public static final String INGEST_PREFIX = "ingest-"; | ||
protected static final String WRONG_ENDPOINT_MESSAGE = "Ingestion failed likely because the wrong endpoint, whose ServiceType %s, was configured, which isn't compatible with the client of type '%s' being used. Initialize the client with the appropriate endpoint URL"; | ||
protected static final String CONFIGURED_ENDPOINT_MESSAGE = "is '%s'"; | ||
protected static final String INDETERMINATE_CONFIGURED_ENDPOINT_MESSAGE = "couldn't be determined"; | ||
|
||
protected void validateEndpointServiceType(String endpoint, String expectedServiceType) | ||
throws IngestionClientException { | ||
if (StringUtils.isBlank(endpointServiceType)) { | ||
endpointServiceType = retrieveServiceType(); | ||
} | ||
if (!expectedServiceType.equals(endpointServiceType)) { | ||
String message; | ||
if (StringUtils.isNotBlank(endpointServiceType)) { | ||
String configuredEndpointMessage = String.format(CONFIGURED_ENDPOINT_MESSAGE, endpointServiceType); | ||
message = String.format(WRONG_ENDPOINT_MESSAGE, configuredEndpointMessage, expectedServiceType); | ||
} else { | ||
message = String.format(WRONG_ENDPOINT_MESSAGE, INDETERMINATE_CONFIGURED_ENDPOINT_MESSAGE, expectedServiceType); | ||
} | ||
suggestedEndpointUri = generateEndpointSuggestion(suggestedEndpointUri, endpoint); | ||
if (StringUtils.isNotBlank(suggestedEndpointUri)) { | ||
message = String.format("%s, which is likely '%s'.", message, suggestedEndpointUri); | ||
} else { | ||
message += "."; | ||
} | ||
throw new IngestionClientException(message); | ||
} | ||
static boolean shouldCompress(CompressionType sourceCompressionType, IngestionProperties.DataFormat dataFormat) { | ||
return (sourceCompressionType == null) && (dataFormat == null || dataFormat.isCompressible()); | ||
} | ||
|
||
protected String generateEndpointSuggestion(String existingSuggestedEndpointUri, String dataSource) { | ||
if (existingSuggestedEndpointUri != null) { | ||
return existingSuggestedEndpointUri; | ||
} | ||
// The default is not passing a suggestion to the exception | ||
String endpointUriToSuggestStr = ""; | ||
if (StringUtils.isNotBlank(dataSource)) { | ||
URIBuilder existingEndpoint; | ||
try { | ||
existingEndpoint = new URIBuilder(dataSource); | ||
endpointUriToSuggestStr = emendEndpointUri(existingEndpoint); | ||
} catch (URISyntaxException e) { | ||
log.warn( | ||
"Since the wrong endpoint was used, we attempted to suggest the correct endpoint. However, we couldn't parse dataSource '{}', so no suggestion can be made.", | ||
dataSource, e); | ||
} catch (IllegalArgumentException e) { | ||
log.warn( | ||
"Since the wrong endpoint was used, we attempted to suggest the correct endpoint. However, the URL is already in the correct format '{}', so no suggestion can be made.", | ||
dataSource, e); | ||
} | ||
static String getIngestionEndpoint(String clusterUrl) { | ||
if (clusterUrl.contains(INGEST_PREFIX)) { | ||
return clusterUrl; | ||
} else { | ||
return clusterUrl.replaceFirst(PROTOCOL_SUFFIX, PROTOCOL_SUFFIX + INGEST_PREFIX); | ||
} | ||
|
||
return endpointUriToSuggestStr; | ||
} | ||
|
||
protected abstract String retrieveServiceType(); | ||
|
||
protected abstract String emendEndpointUri(URIBuilder existingEndpoint); | ||
|
||
static boolean shouldCompress(CompressionType sourceCompressionType, IngestionProperties.DataFormat dataFormat) { | ||
return (sourceCompressionType == null) && (dataFormat == null || dataFormat.isCompressible()); | ||
static String getQueryEndpoint(String clusterUrl) { | ||
if (clusterUrl.contains(INGEST_PREFIX)) { | ||
return clusterUrl.replaceFirst(INGEST_PREFIX, ""); | ||
} else { | ||
return clusterUrl; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.