org.apache.distributedlog.client.DistributedLogClientImpl Maven / Gradle / Ivy
The newest version!
/**
* 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.distributedlog.client;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.base.Stopwatch;
import com.google.common.collect.Sets;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.apache.distributedlog.DLSN;
import org.apache.distributedlog.LogRecordSetBuffer;
import org.apache.distributedlog.client.monitor.MonitorServiceClient;
import org.apache.distributedlog.client.ownership.OwnershipCache;
import org.apache.distributedlog.client.proxy.ClusterClient;
import org.apache.distributedlog.client.proxy.HostProvider;
import org.apache.distributedlog.client.proxy.ProxyClient;
import org.apache.distributedlog.client.proxy.ProxyClientManager;
import org.apache.distributedlog.client.proxy.ProxyListener;
import org.apache.distributedlog.client.resolver.RegionResolver;
import org.apache.distributedlog.client.routing.RoutingService;
import org.apache.distributedlog.client.routing.RoutingService.RoutingContext;
import org.apache.distributedlog.client.stats.ClientStats;
import org.apache.distributedlog.client.stats.OpStats;
import org.apache.distributedlog.exceptions.DLClientClosedException;
import org.apache.distributedlog.exceptions.DLException;
import org.apache.distributedlog.exceptions.ServiceUnavailableException;
import org.apache.distributedlog.exceptions.StreamUnavailableException;
import org.apache.distributedlog.service.DLSocketAddress;
import org.apache.distributedlog.service.DistributedLogClient;
import org.apache.distributedlog.thrift.service.BulkWriteResponse;
import org.apache.distributedlog.thrift.service.HeartbeatOptions;
import org.apache.distributedlog.thrift.service.ResponseHeader;
import org.apache.distributedlog.thrift.service.ServerInfo;
import org.apache.distributedlog.thrift.service.ServerStatus;
import org.apache.distributedlog.thrift.service.StatusCode;
import org.apache.distributedlog.thrift.service.WriteContext;
import org.apache.distributedlog.thrift.service.WriteResponse;
import org.apache.distributedlog.util.ProtocolUtils;
import com.twitter.finagle.CancelledRequestException;
import com.twitter.finagle.ConnectionFailedException;
import com.twitter.finagle.Failure;
import com.twitter.finagle.NoBrokersAvailableException;
import com.twitter.finagle.RequestTimeoutException;
import com.twitter.finagle.ServiceException;
import com.twitter.finagle.ServiceTimeoutException;
import com.twitter.finagle.WriteException;
import com.twitter.finagle.builder.ClientBuilder;
import com.twitter.finagle.stats.StatsReceiver;
import com.twitter.finagle.thrift.ClientId;
import com.twitter.util.Duration;
import com.twitter.util.Function;
import com.twitter.util.Function0;
import com.twitter.util.Future;
import com.twitter.util.FutureEventListener;
import com.twitter.util.Promise;
import com.twitter.util.Return;
import com.twitter.util.Throw;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.thrift.TApplicationException;
import org.jboss.netty.channel.ChannelException;
import org.jboss.netty.util.HashedWheelTimer;
import org.jboss.netty.util.Timeout;
import org.jboss.netty.util.TimerTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.collection.Seq;
import scala.runtime.AbstractFunction1;
/**
* Implementation of distributedlog client.
*/
public class DistributedLogClientImpl implements DistributedLogClient, MonitorServiceClient,
RoutingService.RoutingListener, ProxyListener, HostProvider {
private static final Logger logger = LoggerFactory.getLogger(DistributedLogClientImpl.class);
private final String clientName;
private final ClientId clientId;
private final ClientConfig clientConfig;
private final RoutingService routingService;
private final ProxyClient.Builder clientBuilder;
private final boolean streamFailfast;
private final Pattern streamNameRegexPattern;
// Timer
private final HashedWheelTimer dlTimer;
// region resolver
private final RegionResolver regionResolver;
// Ownership maintenance
private final OwnershipCache ownershipCache;
// Channel/Client management
private final ProxyClientManager clientManager;
// Cluster Client (for routing service)
private final Optional clusterClient;
// Close Status
private boolean closed = false;
private final ReentrantReadWriteLock closeLock =
new ReentrantReadWriteLock();
abstract class StreamOp implements TimerTask {
final String stream;
final AtomicInteger tries = new AtomicInteger(0);
final RoutingContext routingContext = RoutingContext.of(regionResolver);
final WriteContext ctx = new WriteContext();
final Stopwatch stopwatch;
final OpStats opStats;
SocketAddress nextAddressToSend;
StreamOp(final String stream, final OpStats opStats) {
this.stream = stream;
this.stopwatch = Stopwatch.createStarted();
this.opStats = opStats;
}
boolean shouldTimeout() {
long elapsedMs = stopwatch.elapsed(TimeUnit.MILLISECONDS);
return shouldTimeout(elapsedMs);
}
boolean shouldTimeout(long elapsedMs) {
return clientConfig.getRequestTimeoutMs() > 0
&& elapsedMs >= clientConfig.getRequestTimeoutMs();
}
void send(SocketAddress address) {
long elapsedMs = stopwatch.elapsed(TimeUnit.MILLISECONDS);
if (clientConfig.getMaxRedirects() > 0
&& tries.get() >= clientConfig.getMaxRedirects()) {
fail(address, new RequestTimeoutException(Duration.fromMilliseconds(elapsedMs),
"Exhausted max redirects in " + elapsedMs + " ms"));
return;
} else if (shouldTimeout(elapsedMs)) {
fail(address, new RequestTimeoutException(Duration.fromMilliseconds(elapsedMs),
"Exhausted max request timeout " + clientConfig.getRequestTimeoutMs()
+ " in " + elapsedMs + " ms"));
return;
}
synchronized (this) {
String addrStr = address.toString();
if (ctx.isSetTriedHosts() && ctx.getTriedHosts().contains(addrStr)) {
nextAddressToSend = address;
dlTimer.newTimeout(this,
Math.min(clientConfig.getRedirectBackoffMaxMs(),
tries.get() * clientConfig.getRedirectBackoffStartMs()),
TimeUnit.MILLISECONDS);
} else {
doSend(address);
}
}
}
abstract Future sendRequest(ProxyClient sc);
void doSend(SocketAddress address) {
ctx.addToTriedHosts(address.toString());
if (clientConfig.isChecksumEnabled()) {
Long crc32 = computeChecksum();
if (null != crc32) {
ctx.setCrc32(crc32);
}
}
tries.incrementAndGet();
sendWriteRequest(address, this);
}
void beforeComplete(ProxyClient sc, ResponseHeader responseHeader) {
ownershipCache.updateOwner(stream, sc.getAddress());
}
void complete(SocketAddress address) {
stopwatch.stop();
opStats.completeRequest(address,
stopwatch.elapsed(TimeUnit.MICROSECONDS), tries.get());
}
void fail(SocketAddress address, Throwable t) {
stopwatch.stop();
opStats.failRequest(address,
stopwatch.elapsed(TimeUnit.MICROSECONDS), tries.get());
}
Long computeChecksum() {
return null;
}
@Override
public synchronized void run(Timeout timeout) throws Exception {
if (!timeout.isCancelled() && null != nextAddressToSend) {
doSend(nextAddressToSend);
} else {
fail(null, new CancelledRequestException());
}
}
}
class BulkWriteOp extends StreamOp {
final List data;
final ArrayList> results;
BulkWriteOp(final String name, final List data) {
super(name, clientStats.getOpStats("bulk_write"));
this.data = data;
// This could take a while (relatively speaking) for very large inputs. We probably don't want
// to go so large for other reasons though.
this.results = new ArrayList>(data.size());
for (int i = 0; i < data.size(); i++) {
checkNotNull(data.get(i));
this.results.add(new Promise());
}
}
@Override
Future sendRequest(final ProxyClient sc) {
return sc.getService().writeBulkWithContext(stream, data, ctx)
.addEventListener(new FutureEventListener() {
@Override
public void onSuccess(BulkWriteResponse response) {
// For non-success case, the ResponseHeader handler (the caller) will handle it.
// Note success in this case means no finagle errors have occurred
// (such as finagle connection issues). In general code != SUCCESS means there's some error
// reported by dlog service. The caller will handle such errors.
if (response.getHeader().getCode() == StatusCode.SUCCESS) {
beforeComplete(sc, response.getHeader());
BulkWriteOp.this.complete(sc.getAddress(), response);
if (response.getWriteResponses().size() == 0 && data.size() > 0) {
logger.error("non-empty bulk write got back empty response without failure for stream {}",
stream);
}
}
}
@Override
public void onFailure(Throwable cause) {
// Handled by the ResponseHeader listener (attached by the caller).
}
}).map(new AbstractFunction1() {
@Override
public ResponseHeader apply(BulkWriteResponse response) {
// We need to return the ResponseHeader to the caller's listener to process DLOG errors.
return response.getHeader();
}
});
}
void complete(SocketAddress address, BulkWriteResponse bulkWriteResponse) {
super.complete(address);
Iterator writeResponseIterator = bulkWriteResponse.getWriteResponses().iterator();
Iterator> resultIterator = results.iterator();
// Fill in errors from thrift responses.
while (resultIterator.hasNext() && writeResponseIterator.hasNext()) {
Promise result = resultIterator.next();
WriteResponse writeResponse = writeResponseIterator.next();
if (StatusCode.SUCCESS == writeResponse.getHeader().getCode()) {
result.setValue(DLSN.deserialize(writeResponse.getDlsn()));
} else {
result.setException(DLException.of(writeResponse.getHeader()));
}
}
// Should never happen, but just in case so there's some record.
if (bulkWriteResponse.getWriteResponses().size() != data.size()) {
logger.error("wrong number of results, response = {} records = {}",
bulkWriteResponse.getWriteResponses().size(), data.size());
}
}
@Override
void fail(SocketAddress address, Throwable t) {
// StreamOp.fail is called to fail the overall request. In case of BulkWriteOp we take the request level
// exception to apply to the first write. In fact for request level exceptions no request has ever been
// attempted, but logically we associate the error with the first write.
super.fail(address, t);
Iterator> resultIterator = results.iterator();
// Fail the first write with the batch level failure.
if (resultIterator.hasNext()) {
Promise result = resultIterator.next();
result.setException(t);
}
// Fail the remaining writes as cancelled requests.
while (resultIterator.hasNext()) {
Promise result = resultIterator.next();
result.setException(new CancelledRequestException());
}
}
@SuppressWarnings("unchecked")
List> result() {
return (List) results;
}
}
abstract class AbstractWriteOp extends StreamOp {
final Promise result = new Promise();
Long crc32 = null;
AbstractWriteOp(final String name, final OpStats opStats) {
super(name, opStats);
}
void complete(SocketAddress address, WriteResponse response) {
super.complete(address);
result.setValue(response);
}
@Override
void fail(SocketAddress address, Throwable t) {
super.fail(address, t);
result.setException(t);
}
@Override
Long computeChecksum() {
if (null == crc32) {
crc32 = ProtocolUtils.streamOpCRC32(stream);
}
return crc32;
}
@Override
Future sendRequest(final ProxyClient sc) {
return this.sendWriteRequest(sc).addEventListener(new FutureEventListener() {
@Override
public void onSuccess(WriteResponse response) {
if (response.getHeader().getCode() == StatusCode.SUCCESS) {
beforeComplete(sc, response.getHeader());
AbstractWriteOp.this.complete(sc.getAddress(), response);
}
}
@Override
public void onFailure(Throwable cause) {
// handled by the ResponseHeader listener
}
}).map(new AbstractFunction1() {
@Override
public ResponseHeader apply(WriteResponse response) {
return response.getHeader();
}
});
}
abstract Future sendWriteRequest(ProxyClient sc);
}
class WriteOp extends AbstractWriteOp {
final ByteBuffer data;
WriteOp(final String name, final ByteBuffer data) {
super(name, clientStats.getOpStats("write"));
this.data = data;
}
@Override
Future sendWriteRequest(ProxyClient sc) {
return sc.getService().writeWithContext(stream, data, ctx);
}
@Override
Long computeChecksum() {
if (null == crc32) {
byte[] dataBytes = new byte[data.remaining()];
data.duplicate().get(dataBytes);
crc32 = ProtocolUtils.writeOpCRC32(stream, dataBytes);
}
return crc32;
}
Future result() {
return result.map(new AbstractFunction1() {
@Override
public DLSN apply(WriteResponse response) {
return DLSN.deserialize(response.getDlsn());
}
});
}
}
class TruncateOp extends AbstractWriteOp {
final DLSN dlsn;
TruncateOp(String name, DLSN dlsn) {
super(name, clientStats.getOpStats("truncate"));
this.dlsn = dlsn;
}
@Override
Long computeChecksum() {
if (null == crc32) {
crc32 = ProtocolUtils.truncateOpCRC32(stream, dlsn);
}
return crc32;
}
@Override
Future sendWriteRequest(ProxyClient sc) {
return sc.getService().truncate(stream, dlsn.serialize(), ctx);
}
Future result() {
return result.map(new AbstractFunction1() {
@Override
public Boolean apply(WriteResponse response) {
return true;
}
});
}
}
class WriteRecordSetOp extends WriteOp {
WriteRecordSetOp(String name, LogRecordSetBuffer recordSet) {
super(name, recordSet.getBuffer());
ctx.setIsRecordSet(true);
}
}
class ReleaseOp extends AbstractWriteOp {
ReleaseOp(String name) {
super(name, clientStats.getOpStats("release"));
}
@Override
Future sendWriteRequest(ProxyClient sc) {
return sc.getService().release(stream, ctx);
}
@Override
void beforeComplete(ProxyClient sc, ResponseHeader header) {
ownershipCache.removeOwnerFromStream(stream, sc.getAddress(), "Stream Deleted");
}
Future result() {
return result.map(new AbstractFunction1() {
@Override
public Void apply(WriteResponse response) {
return null;
}
});
}
}
class DeleteOp extends AbstractWriteOp {
DeleteOp(String name) {
super(name, clientStats.getOpStats("delete"));
}
@Override
Future sendWriteRequest(ProxyClient sc) {
return sc.getService().delete(stream, ctx);
}
@Override
void beforeComplete(ProxyClient sc, ResponseHeader header) {
ownershipCache.removeOwnerFromStream(stream, sc.getAddress(), "Stream Deleted");
}
Future result() {
return result.map(new AbstractFunction1() {
@Override
public Void apply(WriteResponse v1) {
return null;
}
});
}
}
class CreateOp extends AbstractWriteOp {
CreateOp(String name) {
super(name, clientStats.getOpStats("create"));
}
@Override
Future sendWriteRequest(ProxyClient sc) {
return sc.getService().create(stream, ctx);
}
@Override
void beforeComplete(ProxyClient sc, ResponseHeader header) {
ownershipCache.updateOwner(stream, sc.getAddress());
}
Future result() {
return result.map(new AbstractFunction1() {
@Override
public Void apply(WriteResponse v1) {
return null;
}
}).voided();
}
}
class HeartbeatOp extends AbstractWriteOp {
HeartbeatOptions options;
HeartbeatOp(String name, boolean sendReaderHeartBeat) {
super(name, clientStats.getOpStats("heartbeat"));
options = new HeartbeatOptions();
options.setSendHeartBeatToReader(sendReaderHeartBeat);
}
@Override
Future sendWriteRequest(ProxyClient sc) {
return sc.getService().heartbeatWithOptions(stream, ctx, options);
}
Future result() {
return result.map(new AbstractFunction1() {
@Override
public Void apply(WriteResponse response) {
return null;
}
});
}
}
// Stats
private final ClientStats clientStats;
public DistributedLogClientImpl(String name,
ClientId clientId,
RoutingService routingService,
ClientBuilder clientBuilder,
ClientConfig clientConfig,
Optional clusterClient,
StatsReceiver statsReceiver,
StatsReceiver streamStatsReceiver,
RegionResolver regionResolver,
boolean enableRegionStats) {
this.clientName = name;
this.clientId = clientId;
this.routingService = routingService;
this.clientConfig = clientConfig;
this.streamFailfast = clientConfig.getStreamFailfast();
this.streamNameRegexPattern = Pattern.compile(clientConfig.getStreamNameRegex());
this.regionResolver = regionResolver;
// Build the timer
this.dlTimer = new HashedWheelTimer(
new ThreadFactoryBuilder().setNameFormat("DLClient-" + name + "-timer-%d").build(),
this.clientConfig.getRedirectBackoffStartMs(),
TimeUnit.MILLISECONDS);
// register routing listener
this.routingService.registerListener(this);
// build the ownership cache
this.ownershipCache = new OwnershipCache(this.clientConfig, this.dlTimer, statsReceiver, streamStatsReceiver);
// Client Stats
this.clientStats = new ClientStats(statsReceiver, enableRegionStats, regionResolver);
// Client Manager
this.clientBuilder = ProxyClient.newBuilder(clientName, clientId, clientBuilder, clientConfig, clientStats);
this.clientManager = new ProxyClientManager(
this.clientConfig, // client config
this.clientBuilder, // client builder
this.dlTimer, // timer
this, // host provider
clientStats); // client stats
this.clusterClient = clusterClient;
this.clientManager.registerProxyListener(this);
// Cache Stats
StatsReceiver cacheStatReceiver = statsReceiver.scope("cache");
Seq numCachedStreamsGaugeName =
scala.collection.JavaConversions.asScalaBuffer(Arrays.asList("num_streams")).toList();
cacheStatReceiver.provideGauge(numCachedStreamsGaugeName, new Function0
© 2015 - 2025 Weber Informatics LLC | Privacy Policy