blob: c935cb4c547948cb8d5d9ca2757c7962193712f3 [file] [log] [blame]
/**
* Copyright 2016 Yahoo Inc.
*
* 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.
*/
package com.yahoo.pulsar.broker;
import java.io.Closeable;
import java.io.IOException;
import org.apache.bookkeeper.client.BookKeeper;
import org.apache.bookkeeper.mledger.ManagedLedgerFactory;
import org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig;
import org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryImpl;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ManagedLedgerClientFactory implements Closeable {
private static final Logger log = LoggerFactory.getLogger(ManagedLedgerClientFactory.class);
private final ManagedLedgerFactory managedLedgerFactory;
private final BookKeeper bkClient;
public ManagedLedgerClientFactory(ServiceConfiguration conf, ZooKeeper zkClient,
BookKeeperClientFactory bookkeeperProvider) throws Exception {
this.bkClient = bookkeeperProvider.create(conf, zkClient);
ManagedLedgerFactoryConfig managedLedgerFactoryConfig = new ManagedLedgerFactoryConfig();
managedLedgerFactoryConfig.setMaxCacheSize(conf.getManagedLedgerCacheSizeMB() * 1024L * 1024L);
managedLedgerFactoryConfig.setCacheEvictionWatermark(conf.getManagedLedgerCacheEvictionWatermark());
this.managedLedgerFactory = new ManagedLedgerFactoryImpl(bkClient, zkClient, managedLedgerFactoryConfig);
}
public ManagedLedgerFactory getManagedLedgerFactory() {
return managedLedgerFactory;
}
public void close() throws IOException {
try {
managedLedgerFactory.shutdown();
log.info("Closed managed ledger factory");
bkClient.close();
log.info("Closed BookKeeper client");
} catch (Exception e) {
log.warn(e.getMessage(), e);
throw new IOException(e);
}
}
}