Skip to content

Commit

Permalink
[fix][sdk] Fixup async task bug
Browse files Browse the repository at this point in the history
  • Loading branch information
chuandew authored and ketor committed May 31, 2024
1 parent 1a78b18 commit 27096fc
Show file tree
Hide file tree
Showing 29 changed files with 290 additions and 198 deletions.
3 changes: 1 addition & 2 deletions src/example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ if(SDK_ENABLE_GRPC)
target_link_libraries(txn_scan_test
${DEP_LIBS}
)

elseif()
else()
add_executable(sdk_rawkv_example
sdk_rawkv_example.cc)
target_link_libraries(sdk_rawkv_example
Expand Down
6 changes: 2 additions & 4 deletions src/pysdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
cmake_minimum_required(VERSION 3.23.1 FATAL_ERROR)
project(pysdk)

find_package(SWIG 4.2 REQUIRED)
find_package(SWIG REQUIRED)

if(SWIG_FOUND)
message("SWIG found: ${SWIG_EXECUTABLE}, dir: ${SWIG_DIR}, version: ${SWIG_VERSION}")
Expand All @@ -29,9 +29,7 @@ include(UseSWIG)
set(CMAKE_SWIG_FLAGS)
set(SWIG_SOURCE_FILE_EXTENSIONS swg)

# list(APPEND CMAKE_SWIG_FLAGS "-DSWIGWORDSIZE64" "-c++")

list(APPEND CMAKE_SWIG_FLAGS "-DSWIGWORDSIZE64" "-c++" "-DSWIG_TYPE_TABLE=aabbccdd")
list(APPEND CMAKE_SWIG_FLAGS "-DSWIGWORDSIZE64" "-c++")

set_property(SOURCE dingosdk.swg PROPERTY CPLUSPLUS ON)

Expand Down
1 change: 1 addition & 0 deletions src/pysdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# we import * so that the symbol X can be accessed as dingosdk.X
from .loader import *
101 changes: 68 additions & 33 deletions src/pysdk/examples/pysdk_rawkv_example.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
#/usr/bin/env python3
# /usr/bin/env python3

from os.path import dirname, abspath
import argparse

import dingosdk

dir = dirname(dirname(dirname(abspath(__file__))))

parser = argparse.ArgumentParser(description='argparse')
parser.add_argument('--coordinator_url', '-url', type=str, default ="file://"+ dir+"/bin/coor_list", help="coordinator url, try to use like file://./coor_list")
parser = argparse.ArgumentParser(description="argparse")
parser.add_argument(
"--coordinator_addrs",
"-addrs",
type=str,
default="127.0.0.1:22001,127.0.0.1:22002,127.0.0.1:22003",
help="coordinator addrs, try to use like 127.0.0.1:22001,127.0.0.1:22002,127.0.0.1:22003",
)
args = parser.parse_args()

g_region_ids=[]
g_region_ids = []

s, g_client = dingosdk.Client.Build(args.coordinator_url)
s, g_client = dingosdk.Client.BuildFromAddrs(args.coordinator_addrs)
assert s.ok(), "client build fail"


def create_region(name: str, start_key: str, end_key: str, replicas: int = 3,
engine_type = dingosdk.kLSM):
def create_region(
name: str,
start_key: str,
end_key: str,
replicas: int = 3,
engine_type=dingosdk.kLSM,
):
assert name, "name should not be empty"
assert start_key, "start_key should not be empty"
assert end_key, "end_key should not be empty"
Expand All @@ -37,7 +46,7 @@ def create_region(name: str, start_key: str, end_key: str, replicas: int = 3,

print(f"Create region status: {s.ToString()}, region_id: {region_id}")

if s.ok() :
if s.ok():
assert region_id > 0
s, inprogress = g_client.IsCreateRegionInProgress(region_id)
assert not inprogress
Expand All @@ -49,7 +58,9 @@ def post_clean():
s = g_client.DropRegion(region_id)
print(f"Drop region status: {s.ToString()}, region_id: {region_id}")
s, inprogress = g_client.IsCreateRegionInProgress(region_id)
print(f"Query region status: {s.ToString()}, region_id: {region_id}, {inprogress}")
print(
f"Query region status: {s.ToString()}, region_id: {region_id}, {inprogress}"
)
g_region_ids.clear()


Expand All @@ -63,13 +74,13 @@ def raw_kv_example():
put = raw_kv.Put(key, value)
print(f"raw_kv put status:{put.ToString()}, key: {key}, value: {value}")

got, to_get= raw_kv.Get(key)
got, to_get = raw_kv.Get(key)
print(f"raw_kv get status:{got.ToString()}, key: {key}, value: {to_get}")

dele = raw_kv.Delete(key)
print(f"raw_kv delete status:{dele.ToString()}, key: {key}")
if dele.ok():
got, tmp= raw_kv.Get(key)
got, tmp = raw_kv.Get(key)
print(f"raw_kv get status:{got.ToString()}, key: {key}, value: {tmp}")

keys = ["wb01", "wc01", "wd01", "wf01", "wl01", "wm01"]
Expand Down Expand Up @@ -111,18 +122,18 @@ def raw_kv_example():
result, state = raw_kv.PutIfAbsent(key, value)
print(f"raw_kv put_if_absent: {result.ToString()}, state: {state}")

result, to_get= raw_kv.Get(key)
result, to_get = raw_kv.Get(key)
print(f"raw_kv get after put_if_absent: {result.ToString()}, value: {to_get}")
if result.ok():
assert value == to_get

result, again_state= raw_kv.PutIfAbsent(key, value)
result, again_state = raw_kv.PutIfAbsent(key, value)
print(f"raw_kv put_if_absent again: {result.ToString()}, state: {again_state}")

result = raw_kv.Delete(key)
print(f"raw_kv delete: {result.ToString()}")
if result.ok():
result, tmp= raw_kv.Get(key)
result, tmp = raw_kv.Get(key)
print(f"raw_kv get after delete: {result.ToString()}, value: {tmp}")

# batch put if absent
Expand All @@ -131,21 +142,27 @@ def raw_kv_example():
print(f"raw_kv batch_put_if_absent: {result.ToString()}")
if result.ok():
for key_state in keys_state:
print(f"raw_kv batch_put_if_absent, key: {key_state.key}, state: {key_state.state}")
print(
f"raw_kv batch_put_if_absent, key: {key_state.key}, state: {key_state.state}"
)

batch_get_values = dingosdk.KVPairVector()
result = raw_kv.BatchGet(keys, batch_get_values)
print(f"raw_kv batch_get after batch_put_if_absent: {result.ToString()}")
if result.ok():
for kv in batch_get_values:
print(f"raw_kv batch_get after batch_put_if_absent, key: {kv.key}, value: {kv.value}")
print(
f"raw_kv batch_get after batch_put_if_absent, key: {kv.key}, value: {kv.value}"
)

again_keys_state = dingosdk.KeyOpStateVector()
result = raw_kv.BatchPutIfAbsent(kvs, again_keys_state)
print(f"raw_kv batch_put_if_absent again: {result.ToString()}")
if result.ok():
for key_state in again_keys_state:
print(f"raw_kv batch_put_if_absent again, key: {key_state.key}, state: {key_state.state}")
print(
f"raw_kv batch_put_if_absent again, key: {key_state.key}, state: {key_state.state}"
)

result = raw_kv.BatchDelete(keys)
print(f"raw_kv batch_delete: {result.ToString()}")
Expand Down Expand Up @@ -173,39 +190,49 @@ def raw_kv_example():
print(f"raw_kv delete range: {result.ToString()}, delete_count: {delete_count}")

result, delete_count = raw_kv.DeleteRangeNonContinuous("wb01", "wz01")
print(f"raw_kv delete range non continuous: {result.ToString()}, delete_count: {delete_count}")
print(
f"raw_kv delete range non continuous: {result.ToString()}, delete_count: {delete_count}"
)

tmp_batch_get_values = dingosdk.KVPairVector()
result = raw_kv.BatchGet(keys, tmp_batch_get_values)
print(f"raw_kv batch_get after delete_range: {result.ToString()}")
if result.ok():
for kv in tmp_batch_get_values:
print(f"raw_kv batch_get after delete_range, key: {kv.key}, value: {kv.value}")
print(
f"raw_kv batch_get after delete_range, key: {kv.key}, value: {kv.value}"
)

# compare and set
key = "wb01"
value = "pong"

result, state= raw_kv.CompareAndSet(key, value, "")
print(f"raw_kv compare_and_set: {result.ToString()}, key: {key}, value: {value}, expect: empty, state: {state}")
result, state = raw_kv.CompareAndSet(key, value, "")
print(
f"raw_kv compare_and_set: {result.ToString()}, key: {key}, value: {value}, expect: empty, state: {state}"
)

result, to_get = raw_kv.Get(key)
print(f"raw_kv get after compare_and_set: {result.ToString()}, value: {to_get}")
if result.ok():
assert value == to_get

result, again_state= raw_kv.CompareAndSet(key, "ping", value)
print(f"raw_kv compare_and_set again: {result.ToString()}, key: {key}, value: ping, expect: {value}, state: {again_state}")
result, again_state = raw_kv.CompareAndSet(key, "ping", value)
print(
f"raw_kv compare_and_set again: {result.ToString()}, key: {key}, value: ping, expect: {value}, state: {again_state}"
)

result, again_get= raw_kv.Get(key)
print(f"raw_kv get after compare_and_set again: {result.ToString()}, value: {again_get}")
result, again_get = raw_kv.Get(key)
print(
f"raw_kv get after compare_and_set again: {result.ToString()}, value: {again_get}"
)
if result.ok():
assert "ping" == again_get

result = raw_kv.Delete(key)
print(f"raw_kv delete: {result.ToString()}")
if result.ok():
result, tmp= raw_kv.Get(key)
result, tmp = raw_kv.Get(key)
print(f"raw_kv get after delete: {result.ToString()}, value: {tmp}")
assert tmp == ""

Expand All @@ -224,15 +251,19 @@ def raw_kv_example():
print(f"raw_kv batch_compare_and_set: {result.ToString()}")
if result.ok():
for key_state in keys_state:
print(f"raw_kv batch_compare_and_set, key: {key_state.key}, state: {key_state.state}")
print(
f"raw_kv batch_compare_and_set, key: {key_state.key}, state: {key_state.state}"
)
assert key_state.state

batch_get_values = dingosdk.KVPairVector()
result = raw_kv.BatchGet(keys, batch_get_values)
print(f"raw_kv batch_get after batch_compare_and_set: {result.ToString()}")
if result.ok():
for kv in batch_get_values:
print(f"raw_kv batch_get after batch_compare_and_set, key: {kv.key}, value: {kv.value}")
print(
f"raw_kv batch_get after batch_compare_and_set, key: {kv.key}, value: {kv.value}"
)
find = False
for ele in kvs:
if ele.key == kv.key:
Expand All @@ -258,15 +289,19 @@ def raw_kv_example():
print(f"raw_kv batch_compare_and_set again: {result.ToString()}")
if result.ok():
for key_state in again_keys_state:
print(f"raw_kv batch_compare_and_set again, key: {key_state.key}, state: {key_state.state}")
print(
f"raw_kv batch_compare_and_set again, key: {key_state.key}, state: {key_state.state}"
)
assert key_state.state

batch_get_values = dingosdk.KVPairVector()
result = raw_kv.BatchGet(keys, batch_get_values)
print(f"raw_kv batch_get after batch_compare_and_set again: {result.ToString()}")
if result.ok():
for kv in batch_get_values:
print(f"raw_kv batch_get after batch_compare_and_set again, key: {kv.key}, value: {kv.value}")
print(
f"raw_kv batch_get after batch_compare_and_set again, key: {kv.key}, value: {kv.value}"
)
find = False
for ele in kvs:
if ele.key == kv.key:
Expand Down Expand Up @@ -325,4 +360,4 @@ def raw_kv_example():
create_region("skd_example03", "we00000000", "wg00000000", 3, dingosdk.kBTree)
create_region("skd_example04", "wl00000000", "wn00000000", 3, dingosdk.kBTree)
raw_kv_example()
post_clean()
post_clean()
Loading

0 comments on commit 27096fc

Please sign in to comment.