rmatharu 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_r273180831
########## 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) { Review comment: Seems like a helper function, which could just be a static. Also, maybe moved down in the class. ---------------------------------------------------------------- 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
