-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
291 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module HStream.Common.ZookeeperClient | ||
( ZookeeperClient | ||
, withZookeeperClient | ||
, unsafeGetZHandle | ||
) where | ||
|
||
import Data.Word | ||
import Foreign.C.Types | ||
import Foreign.ForeignPtr | ||
import Foreign.Ptr | ||
import Unsafe.Coerce (unsafeCoerce) | ||
import Z.Data.CBytes (CBytes, withCBytes) | ||
import ZooKeeper.Types (ZHandle) | ||
|
||
newtype ZookeeperClient = ZookeeperClient (Ptr CZookeeperClient) | ||
|
||
withZookeeperClient :: CBytes -> CInt -> (ZookeeperClient -> IO a) -> IO a | ||
withZookeeperClient quorum timeout f = do | ||
fp <- newZookeeperClient quorum timeout | ||
withForeignPtr fp $ f . ZookeeperClient | ||
|
||
newZookeeperClient :: CBytes -> CInt -> IO (ForeignPtr CZookeeperClient) | ||
newZookeeperClient quorum timeout = do | ||
withCBytes quorum $ \quorum' -> do | ||
client <- new_zookeeper_client quorum' timeout | ||
newForeignPtr delete_zookeeper_client client | ||
|
||
-- It's safe to use unsafeGetZHandle in hstream since we donot free the | ||
-- ZookeeperClient in the server lifetime. | ||
unsafeGetZHandle :: ZookeeperClient -> IO ZHandle | ||
unsafeGetZHandle (ZookeeperClient ptr) = | ||
-- TODO: let zoovisitor exports the ZHandle constructor | ||
-- | ||
-- It's safe to use unsafeCoerce here because the ZHandle is a newtype. | ||
unsafeCoerce <$> get_underlying_handle ptr | ||
|
||
data CZookeeperClient | ||
|
||
foreign import ccall safe "new_zookeeper_client" | ||
new_zookeeper_client :: Ptr Word8 -> CInt -> IO (Ptr CZookeeperClient) | ||
|
||
foreign import ccall unsafe "&delete_zookeeper_client" | ||
delete_zookeeper_client :: FunPtr (Ptr CZookeeperClient -> IO ()) | ||
|
||
foreign import ccall unsafe "get_underlying_handle" | ||
get_underlying_handle :: Ptr CZookeeperClient -> IO (Ptr ()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <logdevice/common/ZookeeperClient.h> | ||
#include <zookeeper/zookeeper.h> | ||
|
||
// We directly use the ZookeeperClient class from LogDevice, it handles | ||
// SessionExpire that we need to handle. | ||
// | ||
// TODO: | ||
// | ||
// 1. Currently logging messages are printed by ld_info, which is controlled by | ||
// `--store-log-level`. We need to find a way to use the hstream logger. | ||
|
||
extern "C" { | ||
|
||
typedef struct zookeeper_client_t { | ||
std::unique_ptr<facebook::logdevice::ZookeeperClient> rep; | ||
} zookeeper_client_t; | ||
|
||
zookeeper_client_t* new_zookeeper_client(const char* quorum, | ||
int session_timeout) { | ||
zookeeper_client_t* client = new zookeeper_client_t(); | ||
client->rep = std::make_unique<facebook::logdevice::ZookeeperClient>( | ||
std::string(quorum), std::chrono::milliseconds(session_timeout)); | ||
return client; | ||
} | ||
|
||
zhandle_t* get_underlying_handle(zookeeper_client_t* client) { | ||
return client->rep->getHandle().get(); | ||
} | ||
|
||
void delete_zookeeper_client(zookeeper_client_t* client) { delete client; } | ||
|
||
// end extern "C" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.