Zhangyx39 commented on a change in pull request #986: SAMZA-2156: Couchbase support for Samza Table API URL: https://github.com/apache/samza/pull/986#discussion_r276028401
########## File path: samza-kv-remote/src/main/java/org/apache/samza/table/remote/couchbase/BaseCouchbaseTableFunction.java ########## @@ -0,0 +1,277 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.samza.table.remote.couchbase; + +import com.couchbase.client.java.Bucket; +import com.couchbase.client.java.error.TemporaryFailureException; +import com.couchbase.client.java.error.TemporaryLockFailureException; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import java.io.Serializable; +import java.time.Duration; +import java.util.List; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.samza.context.Context; +import org.apache.samza.operators.functions.ClosableFunction; +import org.apache.samza.operators.functions.InitableFunction; +import org.apache.samza.serializers.Serde; + + +/** + * Base class for {@link CouchbaseTableReadFunction} and {@link CouchbaseTableWriteFunction} + * @param <V> Type of values to read from / write to couchbase + */ +public abstract class BaseCouchbaseTableFunction<V> implements InitableFunction, ClosableFunction, Serializable { + + // Clients + private final static CouchbaseBucketRegistry COUCHBASE_BUCKET_REGISTRY = new CouchbaseBucketRegistry(); + protected transient Bucket bucket; + + // Function Settings + protected Serde<V> valueSerde = null; + protected Duration timeout = Duration.ZERO; // default value 0 means no timeout + protected Duration ttl = Duration.ZERO; // default value 0 means no ttl, data will be stored forever + + // Cluster Settings + protected final List<String> clusterNodes; + protected final String bucketName; + + // Environment Settings + protected CouchbaseEnvironmentConfigs environmentConfigs; + + /** + * Constructor for BaseCouchbaseTableFunction. This constructor abstracts the shareable logic of the read and write + * functions. It is not intended to be called directly. + * @param bucketName Name of the Couchbase bucket + * @param clusterNodes Some Hosts of the Couchbase cluster. Recommended to provide more than one nodes so that if + * the first node could not be connected, other nodes can be tried. + * @param valueClass type of values + */ + public BaseCouchbaseTableFunction(String bucketName, List<String> clusterNodes, Class<V> valueClass) { + Preconditions.checkArgument(StringUtils.isNotEmpty(bucketName), "Bucket name is not allowed to be null or empty."); + Preconditions.checkArgument(CollectionUtils.isNotEmpty(clusterNodes), + "Cluster nodes is not allowed to be null or empty."); + Preconditions.checkArgument(valueClass != null, "Value class is not allowed to be null."); + this.bucketName = bucketName; + this.clusterNodes = ImmutableList.copyOf(clusterNodes); + this.environmentConfigs = new CouchbaseEnvironmentConfigs(); + } + + /** + * Helper method to initialize {@link Bucket}. + */ + @Override + public void init(Context context) { + bucket = COUCHBASE_BUCKET_REGISTRY.getBucket(bucketName, clusterNodes, environmentConfigs); + } + + /** + * {@inheritDoc} + */ + @Override + public void close() { + COUCHBASE_BUCKET_REGISTRY.closeBucket(bucketName, clusterNodes); + } + + /** + * Check whether the exception is caused by one of the temporary failure exceptions, which are + * likely to be retriable. + * @param exception exception thrown by the table provider + * @return a boolean + */ + public boolean isRetriable(Throwable exception) { + while (exception != null && !(exception instanceof TemporaryFailureException) + && !(exception instanceof TemporaryLockFailureException)) { + exception = exception.getCause(); + } + return exception != null; + } + + /** + * Set the timeout limit on the read / write operations. Default value is Duration.ZERO, which means no timeout. + * See <a href="https://docs.couchbase.com/java-sdk/2.7/client-settings.html#timeout-options"></a>. + * @param timeout Timeout duration + * @param <T> type of this instance + * @return Self + */ + public <T extends BaseCouchbaseTableFunction<V>> T withTimeout(Duration timeout) { + this.timeout = timeout; + return (T) this; + } + + /** + * Set the TTL for the data writen to Couchbase. Default value Duration.ZERO means no TTL, data will be stored forever. + * See <a href="https://docs.couchbase.com/java-sdk/2.7/core-operations.html#expiry"></a>. + * @param ttl TTL duration + * @param <T> type of this instance + * @return Self + */ + public <T extends BaseCouchbaseTableFunction<V>> T withTtl(Duration ttl) { + this.ttl = ttl; + return (T) this; + } + + /** + * Serde is used to serialize and deserialize values to/from byte array. If value type is not + * {@link com.couchbase.client.java.document.json.JsonObject}, a Serde must be provided. + * @param valueSerde value serde + * @param <T> type of this instance + * @return Self + */ + public <T extends BaseCouchbaseTableFunction<V>> T withSerde(Serde<V> valueSerde) { + this.valueSerde = valueSerde; + return (T) this; + } + + /** + * Enable role-based authentication with username and password. Note that role-based and certificate-based + * authentications can not be used together. + * @param username username + * @param password password + * @param <T> type of this instance + * @return Self + */ + public <T extends BaseCouchbaseTableFunction<V>> T withUsernameAndPassword(String username, String password) { + if (environmentConfigs.sslEnabled != null && environmentConfigs.sslEnabled) { + throw new IllegalArgumentException( + "Role-Based Access Control and Certificate-Based Authentication cannot be used together."); + } + environmentConfigs.username = username; + environmentConfigs.password = password; + return (T) this; + } + + /** + * Enable certificate-based authentication. If ssl is enabled sslKeystore or sslTrustStore should also be provided + * accordingly. + * @param sslEnabled allows to enable certificate-based authentication + * @param certAuthEnabled allows to enable X.509 client certificate authentication + * @param <T> type of this instance + * @return Self + */ + public <T extends BaseCouchbaseTableFunction<V>> T withSslEnabledAndCertAuthEnabled(boolean sslEnabled, Review comment: There are four combinations here: - Both true, enable certificate-based authentication - Both false, disable - sslEnabled: true, certAuthEnabled: false, only server side certificate checking is needed - sslEnabled: false, certAuthEnabled: true, not allowed, which is checked in line 176 ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
