<commit_before>/**
 * Copyright (c) 2011 Yahoo! Inc. All rights reserved.
 *
 * Licensed 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. See accompanying LICENSE file.
 */

package com.yahoo.pasc.paxos.client;

import java.io.Closeable;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.execution.ExecutionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yahoo.pasc.Message;
import com.yahoo.pasc.PascRuntime;
import com.yahoo.pasc.paxos.Barrier;
import com.yahoo.pasc.paxos.client.messages.Received;
import com.yahoo.pasc.paxos.client.messages.Submit;
import com.yahoo.pasc.paxos.client.messages.Timeout;
import com.yahoo.pasc.paxos.messages.ControlMessage;
import com.yahoo.pasc.paxos.messages.Hello;
import com.yahoo.pasc.paxos.messages.InlineRequest;
import com.yahoo.pasc.paxos.messages.InvalidMessage;
import com.yahoo.pasc.paxos.messages.Request;
import com.yahoo.pasc.paxos.messages.ServerHello;
import com.yahoo.pasc.paxos.messages.serialization.ManualDecoder;
import com.yahoo.pasc.paxos.messages.serialization.ManualEncoder;

public class PaxosClientHandler extends SimpleChannelUpstreamHandler implements PaxosInterface, Watcher {

    private static final Logger LOG = LoggerFactory.getLogger(PaxosClientHandler.class);
    private static final int MAX_CLIENTS = 4096;

    private int clientId;
    private int clients;
    private int timeout;
    private ZooKeeper zk;
    private int warmup = 20000;
    private long measuringTime = 30000;
    private String[] servers;
    private volatile Channel[] serverChannels;
    private volatile int leader = 0;
    private PascRuntime<ClientState> runtime;
    private ClientInterface clientInterface;
    private ChannelFactory channelFactory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool());
    private ChannelPipelineFactory channelPipelineFactory;

    /**
     * Creates a client-side handler.
     * 
     * @throws IOException
     */
    public PaxosClientHandler(final PascRuntime<ClientState> runtime, ClientInterface clientInterface, String[] servers,
            int clients, int timeout, String zkConnection, final ExecutionHandler executor)
            throws IOException {
        this.clients = clients;
        this.timeout = timeout;
        this.zk = new ZooKeeper(zkConnection, 5000, this);
        this.runtime = runtime;
        this.clientInterface = clientInterface;
        this.clientInterface.setInterface(this);
        this.channelPipelineFactory = new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() throws Exception {
                return Channels.pipeline(new ManualEncoder(), new ManualDecoder(runtime.isProtected()), executor,
                        PaxosClientHandler.this);
            }
        };
        this.servers = servers;
        this.serverChannels = new Channel[servers.length];
        Arrays.fill(payload, (byte) 5);
        generateClientId();
        for (int i = 0; i < servers.length; ++i) {
            tryConnect(i);
        }
    }

    private int period = 100;
    private byte[] payload = new byte[1];
    private int resubmits;
    private Timer timer = new Timer("Resubmit Thread");
    private TimerTask resubmit;
    private boolean measureLatency = false;
    private long totalTime = 0;
    private long pendingSentTime = 0;
    private int latencyReceived = 0;

    class ResubmitTask extends TimerTask {
        long timestamp;

        public ResubmitTask(long timestamp) {
            this.timestamp = timestamp;
        }

        @Override
        public void run() {
            resubmitRequest(timestamp);
        }
    }

    @Override
    public synchronized void submitNewRequest(byte[] request) {
        if (resubmit != null)
            resubmit.cancel();
        List<Message> messages = runtime.handleMessage(new Submit(request));
        if (messages == null)
            return;
        for (Message m : messages) {
            LOG.trace("Sending {}", m);
            if (m instanceof InlineRequest) {
                send(m);
            } else {
                sendAll(m);
            }
            pendingSentTime = System.nanoTime();
            // System.out.println("NOT RESUBMITTING");
            resubmit = new ResubmitTask(((Request) m).getTimestamp());
            timer.schedule(resubmit, timeout);
        }
    }

    public void close() {
        if (timer != null)
            timer.cancel();
        if (resubmit != null)
            resubmit.cancel();
        for (Channel c : serverChannels) {
            if (c != null) {
                c.close();
            }
        }
        if (connectionThread != null) {
            connectionThread.interrupt();
        }
    }

    @Override
    public synchronized void submitControlMessage(byte[] controlMessage) {
        ControlMessage cm = new ControlMessage(clientId, controlMessage);
        cm.storeReplica(cm);
        send(cm);
    }

    private static final String ELECTION_PATH = "/pasc_election";
    private Thread connectionThread;<commit_msg>Send control messages to everyone<commit_after>/**
 * Copyright (c) 2011 Yahoo! Inc. All rights reserved.
 *
 * Licensed 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. See accompanying LICENSE file.
 */

package com.yahoo.pasc.paxos.client;

import java.io.Closeable;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.execution.ExecutionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yahoo.pasc.Message;
import com.yahoo.pasc.PascRuntime;
import com.yahoo.pasc.paxos.Barrier;
import com.yahoo.pasc.paxos.client.messages.Received;
import com.yahoo.pasc.paxos.client.messages.Submit;
import com.yahoo.pasc.paxos.client.messages.Timeout;
import com.yahoo.pasc.paxos.messages.ControlMessage;
import com.yahoo.pasc.paxos.messages.Hello;
import com.yahoo.pasc.paxos.messages.InlineRequest;
import com.yahoo.pasc.paxos.messages.InvalidMessage;
import com.yahoo.pasc.paxos.messages.Request;
import com.yahoo.pasc.paxos.messages.ServerHello;
import com.yahoo.pasc.paxos.messages.serialization.ManualDecoder;
import com.yahoo.pasc.paxos.messages.serialization.ManualEncoder;

public class PaxosClientHandler extends SimpleChannelUpstreamHandler implements PaxosInterface, Watcher {

    private static final Logger LOG = LoggerFactory.getLogger(PaxosClientHandler.class);
    private static final int MAX_CLIENTS = 4096;

    private int clientId;
    private int clients;
    private int timeout;
    private ZooKeeper zk;
    private int warmup = 20000;
    private long measuringTime = 30000;
    private String[] servers;
    private volatile Channel[] serverChannels;
    private volatile int leader = 0;
    private PascRuntime<ClientState> runtime;
    private ClientInterface clientInterface;
    private ChannelFactory channelFactory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool());
    private ChannelPipelineFactory channelPipelineFactory;

    /**
     * Creates a client-side handler.
     * 
     * @throws IOException
     */
    public PaxosClientHandler(final PascRuntime<ClientState> runtime, ClientInterface clientInterface, String[] servers,
            int clients, int timeout, String zkConnection, final ExecutionHandler executor)
            throws IOException {
        this.clients = clients;
        this.timeout = timeout;
        this.zk = new ZooKeeper(zkConnection, 5000, this);
        this.runtime = runtime;
        this.clientInterface = clientInterface;
        this.clientInterface.setInterface(this);
        this.channelPipelineFactory = new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() throws Exception {
                return Channels.pipeline(new ManualEncoder(), new ManualDecoder(runtime.isProtected()), executor,
                        PaxosClientHandler.this);
            }
        };
        this.servers = servers;
        this.serverChannels = new Channel[servers.length];
        Arrays.fill(payload, (byte) 5);
        generateClientId();
        for (int i = 0; i < servers.length; ++i) {
            tryConnect(i);
        }
    }

    private int period = 100;
    private byte[] payload = new byte[1];
    private int resubmits;
    private Timer timer = new Timer("Resubmit Thread");
    private TimerTask resubmit;
    private boolean measureLatency = false;
    private long totalTime = 0;
    private long pendingSentTime = 0;
    private int latencyReceived = 0;

    class ResubmitTask extends TimerTask {
        long timestamp;

        public ResubmitTask(long timestamp) {
            this.timestamp = timestamp;
        }

        @Override
        public void run() {
            resubmitRequest(timestamp);
        }
    }

    @Override
    public synchronized void submitNewRequest(byte[] request) {
        if (resubmit != null)
            resubmit.cancel();
        List<Message> messages = runtime.handleMessage(new Submit(request));
        if (messages == null)
            return;
        for (Message m : messages) {
            LOG.trace("Sending {}", m);
            if (m instanceof InlineRequest) {
                send(m);
            } else {
                sendAll(m);
            }
            pendingSentTime = System.nanoTime();
            // System.out.println("NOT RESUBMITTING");
            resubmit = new ResubmitTask(((Request) m).getTimestamp());
            timer.schedule(resubmit, timeout);
        }
    }

    public void close() {
        if (timer != null)
            timer.cancel();
        if (resubmit != null)
            resubmit.cancel();
        for (Channel c : serverChannels) {
            if (c != null) {
                c.close();
            }
        }
        if (connectionThread != null) {
            connectionThread.interrupt();
        }
    }

    @Override
    public synchronized void submitControlMessage(byte[] controlMessage) {
        ControlMessage cm = new ControlMessage(clientId, controlMessage);
        cm.storeReplica(cm);
        sendAll(cm);
    }

    private static final String ELECTION_PATH = "/pasc_election";
    private Thread connectionThread;
{'fraction_non_alphanumeric': 0.09228980452002035, 'fraction_numerical': 0.005522854443717753, 'mean_word_length': 3.1614756576957967, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5662027', 'n_tokens_mistral': 3956, 'n_tokens_neox': 3788, 'n_words': 943}