Search in sources :

Example 1 with SmartClientProtocol

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());
        }
    }
}
Also used : SmartClientProtocol(org.smartdata.protocol.SmartClientProtocol) ConnectException(java.net.ConnectException)

Example 2 with SmartClientProtocol

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("/") ? "" : "/"));
    }
}
Also used : SmartClientProtocol(org.smartdata.protocol.SmartClientProtocol) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) ClientProtocolClientSideTranslator(org.smartdata.protocol.protobuffer.ClientProtocolClientSideTranslator) ClientProtocolProtoBuffer(org.smartdata.protocol.protobuffer.ClientProtocolProtoBuffer)

Example 3 with SmartClientProtocol

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);
}
Also used : SmartClientProtocol(org.smartdata.protocol.SmartClientProtocol) LinkedList(java.util.LinkedList)

Example 4 with SmartClientProtocol

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!");
    }
}
Also used : IOException(java.io.IOException) Callable(java.util.concurrent.Callable) SmartClientProtocol(org.smartdata.protocol.SmartClientProtocol) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with SmartClientProtocol

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());
        }
    }
}
Also used : SmartClientProtocol(org.smartdata.protocol.SmartClientProtocol) NormalFileState(org.smartdata.model.NormalFileState) ConnectException(java.net.ConnectException)

Aggregations

SmartClientProtocol (org.smartdata.protocol.SmartClientProtocol)6 ConnectException (java.net.ConnectException)2 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 TimeoutException (java.util.concurrent.TimeoutException)1 NormalFileState (org.smartdata.model.NormalFileState)1 ClientProtocolClientSideTranslator (org.smartdata.protocol.protobuffer.ClientProtocolClientSideTranslator)1 ClientProtocolProtoBuffer (org.smartdata.protocol.protobuffer.ClientProtocolProtoBuffer)1