/**
 * 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.zabkv;

import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.Properties;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.LinkedBlockingQueue;
import javax.servlet.AsyncContext;
import javax.servlet.http.HttpServletResponse;
import org.apache.zab.SingleNodeZab;
import org.apache.zab.StateMachine;
import org.apache.zab.Zxid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * state machine.
 */
public final class Database implements StateMachine {
  private static final Logger LOG = LoggerFactory.getLogger(Database.class);

  private SingleNodeZab zab;

  private ConcurrentSkipListMap<String, byte[]> kvstore =
    new ConcurrentSkipListMap<>();

  private LinkedBlockingQueue<AsyncContext> pending =
    new LinkedBlockingQueue<>();

  public Database() {
    try {
      zab = new SingleNodeZab(this, new Properties());
    } catch (IOException ex) {
      throw new RuntimeException();
    }
  }

  public byte[] get(String key) {
    return (byte[])kvstore.get((Object)key);
  }

  public byte[] put(String key, byte[] value) {
    return kvstore.put(key, value);
  }

  /**
   * Add a request to this database.
   *
   * This method must be synchronized to ensure that the requests are sent to
   * Zab in the same order they get enqueued to the pending queue.
   */
  public synchronized boolean add(PutCommand command, AsyncContext context) {
    if (!pending.add(context)) {
      return false;
    }
    try {
      zab.send(command.toByteBuffer());
    } catch (IOException ex) {
      throw new RuntimeException();
    }
    return true;
  }

  public ByteBuffer preprocess(Zxid zxid, ByteBuffer message) {
    return message;
  }

  public void deliver(Zxid zxid, ByteBuffer stateUpdate) {
    PutCommand command = PutCommand.fromByteBuffer(stateUpdate);
    LOG.debug("Delivering a command: {} {}", zxid, command);
    command.execute(this);
    AsyncContext context = pending.poll();
    if (context == null) {
      // There is no pending HTTP request to respond to.
      return;
    }
    HttpServletResponse response =
      (HttpServletResponse)(context.getResponse());
    response.setContentType("text/html");
    response.setStatus(HttpServletResponse.SC_OK);
    context.complete();
  }

  public void getState(OutputStream os) {
    throw new UnsupportedOperationException();
  }

  public void setState(InputStream is) {
    throw new UnsupportedOperationException();
  }
}

@@ -1174,26 +1174,22 @@
 che.zab.
-SingleNode
+Quorum
 Zab;%0Aimp
@@ -1221,16 +1221,43 @@
 achine;%0A
+import org.apache.zab.Zab;%0A
 import o
@@ -1502,26 +1502,16 @@
 private 
-SingleNode
 Zab zab;
@@ -1736,54 +1736,187 @@
     
-zab = new SingleNodeZab(this, new Properties()
+Properties prop = new Properties();%0A      prop.setProperty(%22serverId%22, %22localhost:7070%22);%0A      prop.setProperty(%22servers%22, %22localhost:7070%22);%0A      zab = new QuorumZab(this, prop
 );%0A 
@@ -2507,24 +2507,116 @@
 %7D%0A    try %7B%0A
+      ByteBuffer bb = command.toByteBuffer();%0A      LOG.debug(%22Sending a message: %7B%7D%22, bb);%0A
       zab.se
@@ -2798,24 +2798,79 @@
  message) %7B%0A
+    LOG.debug(%22Preprocessing a message: %7B%7D%22, message);%0A
     return m
@@ -2937,24 +2937,78 @@
 teUpdate) %7B%0A
+    LOG.debug(%22Received a message: %7B%7D%22, stateUpdate);%0A
     PutComma

Replace single node zab with quorum zab.