PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/third_party/zookeeper-3.3.1/src/contrib/bookkeeper/src/java/org/apache/bookkeeper/proto/BookieClient.java

https://github.com/hammer/mesos
Java | 178 lines | 122 code | 25 blank | 31 comment | 15 complexity | a93418e08142dbce6bca229808a3bbfe MD5 | raw file
  1. package org.apache.bookkeeper.proto;
  2. /*
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. */
  22. import java.io.IOException;
  23. import java.net.InetSocketAddress;
  24. import java.util.concurrent.ConcurrentHashMap;
  25. import java.util.concurrent.Executors;
  26. import java.util.concurrent.atomic.AtomicLong;
  27. import org.apache.bookkeeper.client.BKException;
  28. import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback;
  29. import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback;
  30. import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.WriteCallback;
  31. import org.apache.bookkeeper.util.OrderedSafeExecutor;
  32. import org.apache.log4j.Logger;
  33. import org.jboss.netty.buffer.ChannelBuffer;
  34. import org.jboss.netty.buffer.ChannelBuffers;
  35. import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
  36. import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
  37. /**
  38. * Implements the client-side part of the BookKeeper protocol.
  39. *
  40. */
  41. public class BookieClient {
  42. static final Logger LOG = Logger.getLogger(BookieClient.class);
  43. // This is global state that should be across all BookieClients
  44. AtomicLong totalBytesOutstanding = new AtomicLong();
  45. OrderedSafeExecutor executor;
  46. ClientSocketChannelFactory channelFactory;
  47. ConcurrentHashMap<InetSocketAddress, PerChannelBookieClient> channels = new ConcurrentHashMap<InetSocketAddress, PerChannelBookieClient>();
  48. public BookieClient(ClientSocketChannelFactory channelFactory, OrderedSafeExecutor executor) {
  49. this.channelFactory = channelFactory;
  50. this.executor = executor;
  51. }
  52. public PerChannelBookieClient lookupClient(InetSocketAddress addr) {
  53. PerChannelBookieClient channel = channels.get(addr);
  54. if (channel == null) {
  55. channel = new PerChannelBookieClient(executor, channelFactory, addr, totalBytesOutstanding);
  56. PerChannelBookieClient prevChannel = channels.putIfAbsent(addr, channel);
  57. if (prevChannel != null) {
  58. channel = prevChannel;
  59. }
  60. }
  61. return channel;
  62. }
  63. public void addEntry(final InetSocketAddress addr, final long ledgerId, final byte[] masterKey, final long entryId,
  64. final ChannelBuffer toSend, final WriteCallback cb, final Object ctx) {
  65. final PerChannelBookieClient client = lookupClient(addr);
  66. client.connectIfNeededAndDoOp(new GenericCallback<Void>() {
  67. @Override
  68. public void operationComplete(int rc, Void result) {
  69. if (rc != BKException.Code.OK) {
  70. cb.writeComplete(rc, ledgerId, entryId, addr, ctx);
  71. return;
  72. }
  73. client.addEntry(ledgerId, masterKey, entryId, toSend, cb, ctx);
  74. }
  75. });
  76. }
  77. public void readEntry(final InetSocketAddress addr, final long ledgerId, final long entryId,
  78. final ReadEntryCallback cb, final Object ctx) {
  79. final PerChannelBookieClient client = lookupClient(addr);
  80. client.connectIfNeededAndDoOp(new GenericCallback<Void>() {
  81. @Override
  82. public void operationComplete(int rc, Void result) {
  83. if (rc != BKException.Code.OK) {
  84. cb.readEntryComplete(rc, ledgerId, entryId, null, ctx);
  85. return;
  86. }
  87. client.readEntry(ledgerId, entryId, cb, ctx);
  88. }
  89. });
  90. }
  91. public void close(){
  92. for (PerChannelBookieClient channel: channels.values()){
  93. channel.close();
  94. }
  95. }
  96. private static class Counter {
  97. int i;
  98. int total;
  99. synchronized void inc() {
  100. i++;
  101. total++;
  102. }
  103. synchronized void dec() {
  104. i--;
  105. notifyAll();
  106. }
  107. synchronized void wait(int limit) throws InterruptedException {
  108. while (i > limit) {
  109. wait();
  110. }
  111. }
  112. synchronized int total() {
  113. return total;
  114. }
  115. }
  116. /**
  117. * @param args
  118. * @throws IOException
  119. * @throws NumberFormatException
  120. * @throws InterruptedException
  121. */
  122. public static void main(String[] args) throws NumberFormatException, IOException, InterruptedException {
  123. if (args.length != 3) {
  124. System.err.println("USAGE: BookieClient bookieHost port ledger#");
  125. return;
  126. }
  127. WriteCallback cb = new WriteCallback() {
  128. public void writeComplete(int rc, long ledger, long entry, InetSocketAddress addr, Object ctx) {
  129. Counter counter = (Counter) ctx;
  130. counter.dec();
  131. if (rc != 0) {
  132. System.out.println("rc = " + rc + " for " + entry + "@" + ledger);
  133. }
  134. }
  135. };
  136. Counter counter = new Counter();
  137. byte hello[] = "hello".getBytes();
  138. long ledger = Long.parseLong(args[2]);
  139. ClientSocketChannelFactory channelFactory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors
  140. .newCachedThreadPool());
  141. OrderedSafeExecutor executor = new OrderedSafeExecutor(1);
  142. BookieClient bc = new BookieClient(channelFactory, executor);
  143. InetSocketAddress addr = new InetSocketAddress(args[0], Integer.parseInt(args[1]));
  144. for (int i = 0; i < 100000; i++) {
  145. counter.inc();
  146. bc.addEntry(addr, ledger, new byte[0], i, ChannelBuffers.wrappedBuffer(hello), cb, counter);
  147. }
  148. counter.wait(0);
  149. System.out.println("Total = " + counter.total());
  150. channelFactory.releaseExternalResources();
  151. executor.shutdown();
  152. }
  153. }