zhaijack commented on a change in pull request #521: Issue 520: Add more http 
endpoint
URL: https://github.com/apache/bookkeeper/pull/521#discussion_r143322959
 
 

 ##########
 File path: 
bookkeeper-server/src/main/java/org/apache/bookkeeper/http/ListLedgerService.java
 ##########
 @@ -0,0 +1,177 @@
+/**
+ *
+ * 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.bookkeeper.http;
+
+import static com.google.common.base.Charsets.UTF_8;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.util.concurrent.AbstractFuture;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.bookkeeper.client.BKException;
+import org.apache.bookkeeper.client.LedgerMetadata;
+import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.http.service.HttpEndpointService;
+import org.apache.bookkeeper.http.service.HttpServiceRequest;
+import org.apache.bookkeeper.http.service.HttpServiceResponse;
+import org.apache.bookkeeper.meta.LedgerManager;
+import org.apache.bookkeeper.meta.LedgerManagerFactory;
+import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks;
+import org.apache.bookkeeper.util.JsonUtil;
+import org.apache.bookkeeper.zookeeper.ZooKeeperClient;
+import org.apache.zookeeper.ZooKeeper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * HttpEndpointService that handle Bookkeeper Configuration related http 
request.
+ */
+public class ListLedgerService implements HttpEndpointService {
+
+    static final Logger LOG = LoggerFactory.getLogger(ListLedgerService.class);
+
+    protected ServerConfiguration conf;
+
+    public ListLedgerService(ServerConfiguration conf) {
+        Preconditions.checkNotNull(conf);
+        this.conf = conf;
+    }
+
+    // Number of LedgerMetadata contains in each page
+    static final int LIST_LEDGER_BATCH_SIZE = 100;
+
+    public static class ReadLedgerMetadataCallback extends 
AbstractFuture<LedgerMetadata>
+      implements BookkeeperInternalCallbacks.GenericCallback<LedgerMetadata> {
+        final long ledgerId;
+
+        ReadLedgerMetadataCallback(long ledgerId) {
+            this.ledgerId = ledgerId;
+        }
+
+        long getLedgerId() {
+            return ledgerId;
+        }
+
+        public void operationComplete(int rc, LedgerMetadata result) {
+            if (rc != 0) {
+                setException(BKException.create(rc));
+            } else {
+                set(result);
+            }
+        }
+    }
+    static void keepLedgerMetadata(ReadLedgerMetadataCallback cb, 
LinkedHashMap<String, String> output) throws Exception {
+        LedgerMetadata md = cb.get();
+        output.put(Long.valueOf(cb.getLedgerId()).toString(), new 
String(md.serialize(), UTF_8));
+    }
+
+    @Override
+    public HttpServiceResponse handle(HttpServiceRequest request) throws 
Exception {
+        HttpServiceResponse response = new HttpServiceResponse();
+        // GET
+        // parameter could be like: print_metadata=true&page=PageIndex
+        if (HttpServer.Method.GET == request.getMethod()) {
+            Map<String, String> params = request.getParams();
+            // default not print metadata
+            boolean printMeta = (params != null) &&
+              params.containsKey("print_metadata") &&
+              params.get("print_metadata").equals("true");
+
+            // Page index should start from 1;
+            int pageIndex = (printMeta && params.containsKey("page")) ?
+              Integer.parseInt(params.get("page")) :
+              -1;
+
+            ZooKeeper zk = ZooKeeperClient.newBuilder()
+              .connectString(conf.getZkServers())
+              .sessionTimeoutMs(conf.getZkTimeout())
+              .build();
+            LedgerManagerFactory mFactory = 
LedgerManagerFactory.newLedgerManagerFactory(conf, zk);
+            LedgerManager manager = mFactory.newLedgerManager();
+            LedgerManager.LedgerRangeIterator iter = manager.getLedgerRanges();
+
+            // output <ledgerId: ledgerMetadata>
+            LinkedHashMap<String, String> output = Maps.newLinkedHashMap();
+            // futures for readLedgerMetadata for each page.
+            List<ReadLedgerMetadataCallback> futures = 
Lists.newArrayListWithExpectedSize(LIST_LEDGER_BATCH_SIZE);
+
+            if (printMeta) {
+                int ledgerIndex = 0;
+
+                // start and end ledger index for wanted page.
+                int startLedgerIndex = 0;
+                int endLedgerIndex = 0;
+                if(pageIndex > 0) {
+                    startLedgerIndex = (pageIndex - 1) * 
LIST_LEDGER_BATCH_SIZE;
+                    endLedgerIndex = startLedgerIndex + LIST_LEDGER_BATCH_SIZE 
- 1;
+                }
+
+                // get metadata
+                while (iter.hasNext()) {
+                    LedgerManager.LedgerRange r = iter.next();
 
 Review comment:
   Thanks, opened issue #542 for tracking it. and would like to keep current 
changes, then later we will only need add needed change.
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to