Skip to content

Commit

Permalink
feat: expose parameter success_if_exist for interface create_app
Browse files Browse the repository at this point in the history
  • Loading branch information
GehaFearless committed Sep 28, 2022
1 parent 689fd6f commit 8b59872
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 15 deletions.
19 changes: 12 additions & 7 deletions go-client/admin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
// Client provides the administration API to a specific cluster.
// Remember only the superusers configured to the cluster have the admin priviledges.
type Client interface {
CreateTable(ctx context.Context, tableName string, partitionCount int) error
CreateTable(ctx context.Context, tableName string, partitionCount int, successIfExist_optional ...bool) error

DropTable(ctx context.Context, tableName string) error

Expand Down Expand Up @@ -88,15 +88,20 @@ func (c *rpcBasedClient) waitTableReady(ctx context.Context, tableName string, p
return nil
}

func (c *rpcBasedClient) CreateTable(ctx context.Context, tableName string, partitionCount int) error {
func (c *rpcBasedClient) CreateTable(ctx context.Context, tableName string, partitionCount int, successIfExist_optional ...bool) error {
successIfExist := true
if len(successIfExist_optional) > 0 {
successIfExist = successIfExist_optional[0]
}
_, err := c.metaManager.CreateApp(ctx, &admin.CreateAppRequest{
AppName: tableName,
Options: &admin.CreateAppOptions{
PartitionCount: int32(partitionCount),
ReplicaCount: 3,
AppType: "pegasus",
Envs: make(map[string]string),
IsStateful: true,
PartitionCount: int32(partitionCount),
ReplicaCount: 3,
SuccessIfExist: successIfExist,
AppType: "pegasus",
Envs: make(map[string]string),
IsStateful: true,
},
})
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public void createApp(
int partitionCount,
int replicaCount,
Map<String, String> envs,
long timeoutMs)
long timeoutMs,
boolean successIfExist)
throws PException {
if (partitionCount < 1) {
throw new PException(
Expand Down Expand Up @@ -111,7 +112,7 @@ public void createApp(
create_app_options options = new create_app_options();
options.setPartition_count(partitionCount);
options.setReplica_count(replicaCount);
options.setSuccess_if_exist(true);
options.setSuccess_if_exist(successIfExist);
options.setApp_type(APP_TYPE);
options.setEnvs(envs);
options.setIs_stateful(true);
Expand Down Expand Up @@ -158,6 +159,17 @@ public void createApp(
}
}

@Override
public void createApp(
String appName,
int partitionCount,
int replicaCount,
Map<String, String> envs,
long timeoutMs)
throws PException {
this.createApp(appName, partitionCount, replicaCount, envs, timeoutMs, true);
}

@Override
public boolean isAppHealthy(String appName, int replicaCount) throws PException {
if (replicaCount < 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,20 @@ public interface PegasusAdminClientInterface {
* @param envs Environment variables of pegasus app, you can see the supported envs in the website
* : https://pegasus.apache.org/administration/table-env
* @param timeoutMs The timeout of the interface, milli-seconds
* @param successIfExist whether return success if app exist
* @throws PException if rpc to the pegasus server cause timeout or other error happens in the
* server side, or the newly created app is not fully healthy when the 'timeoutMs' has
* elapsed, the interface will throw exception
*/
public void createApp(
String appName,
int partitionCount,
int replicaCount,
Map<String, String> envs,
long timeoutMs,
boolean successIfExist)
throws PException;

public void createApp(
String appName,
int partitionCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ class replication_ddl_client
int partition_count,
int replica_count,
const std::map<std::string, std::string> &envs,
bool is_stateless);
bool is_stateless,
bool success_if_exist = true);

// reserve_seconds == 0 means use default value in configuration 'hold_seconds_for_dropped_app'
dsn::error_code drop_app(const std::string &app_name, int reserve_seconds);
Expand Down
5 changes: 3 additions & 2 deletions src/rdsn/src/client/replication_ddl_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ dsn::error_code replication_ddl_client::create_app(const std::string &app_name,
int partition_count,
int replica_count,
const std::map<std::string, std::string> &envs,
bool is_stateless)
bool is_stateless,
bool success_if_exist)
{
if (partition_count < 1) {
std::cout << "create app " << app_name << " failed: partition_count should >= 1"
Expand Down Expand Up @@ -157,7 +158,7 @@ dsn::error_code replication_ddl_client::create_app(const std::string &app_name,
req->app_name = app_name;
req->options.partition_count = partition_count;
req->options.replica_count = replica_count;
req->options.success_if_exist = true;
req->options.success_if_exist = success_if_exist;
req->options.app_type = app_type;
req->options.envs = envs;
req->options.is_stateful = !is_stateless;
Expand Down
10 changes: 8 additions & 2 deletions src/shell/commands/table_management.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,21 +631,23 @@ bool create_app(command_executor *e, shell_context *sc, arguments args)
{
static struct option long_options[] = {{"partition_count", required_argument, 0, 'p'},
{"replica_count", required_argument, 0, 'r'},
{"fail_if_exist", no_argument, 0, 's'},
{"envs", required_argument, 0, 'e'},
{0, 0, 0, 0}};

if (args.argc < 2)
return false;

std::string app_name = args.argv[1];
bool success_if_exist = true;

int pc = 4, rc = 3;
std::map<std::string, std::string> envs;
optind = 0;
while (true) {
int option_index = 0;
int c;
c = getopt_long(args.argc, args.argv, "p:r:e:", long_options, &option_index);
c = getopt_long(args.argc, args.argv, "p:r:fe:", long_options, &option_index);
if (c == -1)
break;
switch (c) {
Expand All @@ -661,6 +663,9 @@ bool create_app(command_executor *e, shell_context *sc, arguments args)
return false;
}
break;
case 'f':
success_if_exist = false;
break;
case 'e':
if (!::dsn::utils::parse_kv_map(optarg, envs, ',', '=')) {
fprintf(stderr, "invalid envs: %s\n", optarg);
Expand All @@ -672,7 +677,8 @@ bool create_app(command_executor *e, shell_context *sc, arguments args)
}
}

::dsn::error_code err = sc->ddl_client->create_app(app_name, "pegasus", pc, rc, envs, false);
::dsn::error_code err =
sc->ddl_client->create_app(app_name, "pegasus", pc, rc, envs, false, success_if_exist);
if (err == ::dsn::ERR_OK)
std::cout << "create app \"" << pegasus::utils::c_escape_string(app_name) << "\" succeed"
<< std::endl;
Expand Down
2 changes: 1 addition & 1 deletion src/shell/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static command_executor commands[] = {
{
"create",
"create an app",
"<app_name> [-p|--partition_count num] [-r|--replica_count num] "
"<app_name> [-p|--partition_count num] [-r|--replica_count num] [-f|--fail_if_exist] "
"[-e|--envs k1=v1,k2=v2...]",
create_app,
},
Expand Down

0 comments on commit 8b59872

Please sign in to comment.