use of org.smartdata.protocol.SmartClientProtocol in project SSM by Intel-bigdata.
the class SmartClient method reportFileAccessEventSimply.
/**
* A simple report strategy that tries to connect to smart server one by one.
* And active smart server address will be updated in a local file for new
* client to use henceforth.
* @param event
* @throws IOException
*/
private void reportFileAccessEventSimply(FileAccessEvent event) throws IOException {
int failedServerNum = 0;
while (true) {
try {
SmartClientProtocol server = serverQue.getFirst();
server.reportFileAccessEvent(event);
if (failedServerNum != 0) {
onNewActiveSmartServer();
}
break;
} catch (ConnectException e) {
failedServerNum++;
// throw an exception.
if (failedServerNum == serverQue.size()) {
throw new ConnectException("Tried to connect to configured SSM " + "server(s), but failed." + e.getMessage());
}
// Move the first server to last.
serverQue.addLast(serverQue.pollFirst());
}
}
}
use of org.smartdata.protocol.SmartClientProtocol in project SSM by Intel-bigdata.
the class SmartClient method initialize.
private void initialize(InetSocketAddress[] addrs) throws IOException {
RPC.setProtocolEngine(conf, ClientProtocolProtoBuffer.class, ProtobufRpcEngine.class);
List<InetSocketAddress> orderedAddrs = new ArrayList<>();
InetSocketAddress recordedActiveAddr = getActiveServerAddress();
if (recordedActiveAddr != null) {
orderedAddrs.add(recordedActiveAddr);
}
for (InetSocketAddress addr : addrs) {
if (!addr.equals(recordedActiveAddr)) {
orderedAddrs.add(addr);
}
}
for (InetSocketAddress addr : orderedAddrs) {
ClientProtocolProtoBuffer proxy = RPC.getProxy(ClientProtocolProtoBuffer.class, VERSION, addr, conf);
SmartClientProtocol server = new ClientProtocolClientSideTranslator(proxy);
serverQue.addLast(server);
serverToRpcAddr.put(server, addr.getHostName() + ":" + addr.getPort());
}
// SMART_IGNORE_DIRS_KEY and SMART_WORK_DIR_KEY should be configured on
// application side if its dfsClient is replaced by SmartDfsClient.
Collection<String> ignoreDirs = conf.getTrimmedStringCollection(SmartConfKeys.SMART_IGNORE_DIRS_KEY);
// The system folder and SSM work folder should be ignored to
// report access count.
ignoreDirs.add(SmartConstants.SYSTEM_FOLDER);
ignoreDirs.add(conf.get(SmartConfKeys.SMART_WORK_DIR_KEY, SmartConfKeys.SMART_WORK_DIR_DEFAULT));
for (String s : ignoreDirs) {
ignoreAccessEventDirs.add(s + (s.endsWith("/") ? "" : "/"));
}
Collection<String> coverDirs = conf.getTrimmedStringCollection(SmartConfKeys.SMART_COVER_DIRS_KEY);
for (String s : coverDirs) {
coverAccessEventDirs.add(s + (s.endsWith("/") ? "" : "/"));
}
}
use of org.smartdata.protocol.SmartClientProtocol in project SSM by Intel-bigdata.
the class SmartClient method onNewActiveSmartServer.
/**
* Reset smart server address in conf and a local file to reflect the
* changes of active smart server in fail over.
*/
public void onNewActiveSmartServer() {
List<String> rpcAddrs = new LinkedList<>();
for (SmartClientProtocol s : serverQue) {
rpcAddrs.add(serverToRpcAddr.get(s));
}
conf.set(SmartConfKeys.SMART_SERVER_RPC_ADDRESS_KEY, StringUtil.join(",", rpcAddrs));
String addr = serverToRpcAddr.get(serverQue.getFirst());
recordActiveServerAddr(addr);
}
use of org.smartdata.protocol.SmartClientProtocol in project SSM by Intel-bigdata.
the class SmartClient method reportFileAccessEventConcurrently.
/**
* Report file access event concurrently. Only one server is active, so
* reporting to this server will be successful.
* @param event
*/
private void reportFileAccessEventConcurrently(FileAccessEvent event) throws IOException {
int num = serverQue.size();
ExecutorService executorService = Executors.newFixedThreadPool(num);
Future<Void>[] futures = new Future[num];
int index = 0;
for (SmartClientProtocol server : serverQue) {
futures[index] = executorService.submit(new Callable<Void>() {
@Override
public Void call() throws IOException {
server.reportFileAccessEvent(event);
return null;
}
});
index++;
}
boolean isReported = false;
byte tryNum = 0;
while (tryNum++ < 10) {
for (Future<Void> future : futures) {
try {
// A short timeout value for performance consideration.
future.get(200, TimeUnit.MILLISECONDS);
isReported = true;
break;
// ExecutionException will be thrown if IOException inside #call is
// thrown. Multiple calling #get with exception thrown behaves
// consistently.
} catch (InterruptedException | ExecutionException | TimeoutException e) {
continue;
}
}
if (isReported) {
break;
}
}
// Cancel the report tasks. No impact on the successfully executed task.
for (Future<Void> future : futures) {
future.cancel(true);
}
if (!isReported) {
throw new IOException("Failed to report access event to Smart Server!");
}
}
use of org.smartdata.protocol.SmartClientProtocol in project SSM by Intel-bigdata.
the class SmartClient method getFileState.
@Override
public FileState getFileState(String filePath) throws IOException {
checkOpen();
int triedServerNum = 0;
while (true) {
try {
SmartClientProtocol server = serverQue.getFirst();
return server.getFileState(filePath);
} catch (ConnectException e) {
triedServerNum++;
// If all servers has been tried, interrupt and throw the exception.
if (triedServerNum == serverQue.size()) {
// client cannot connect to server
// don't report access event for this file this time
singleIgnoreList.put(filePath, 0);
// the file is compacted or compressed by SSM.
return new NormalFileState(filePath);
}
// Put the first server to last, and will pick the second one to try.
serverQue.addLast(serverQue.pollFirst());
}
}
}
Aggregations