deploy.cpp 3.4 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
Shengliang Guan 已提交
16
#include "deploy.h"
S
Shengliang Guan 已提交
17 18 19 20 21 22 23 24 25

void initLog(char *path) {
  mDebugFlag = 207;
  char temp[PATH_MAX];
  snprintf(temp, PATH_MAX, "%s/taosdlog", path);
  if (taosInitLog(temp, tsNumOfLogLines, 1) != 0) {
    printf("failed to init log file\n");
  }
}
S
Shengliang Guan 已提交
26 27

void* runServer(void* param) {
S
Shengliang Guan 已提交
28
  SServer* pServer = (SServer*)param;
S
Shengliang Guan 已提交
29 30 31 32 33 34
  while (1) {
    taosMsleep(100);
    pthread_testcancel();
  }
}

S
Shengliang Guan 已提交
35
void initOption(SDnodeOpt* pOption, char *path) {
S
Shengliang Guan 已提交
36 37 38 39 40 41 42 43 44 45 46 47
  pOption->sver = 1;
  pOption->numOfCores = 1;
  pOption->numOfSupportMnodes = 1;
  pOption->numOfSupportVnodes = 1;
  pOption->numOfSupportQnodes = 1;
  pOption->statusInterval = 1;
  pOption->mnodeEqualVnodeNum = 1;
  pOption->numOfThreadsPerCore = 1;
  pOption->ratioOfQueryCores = 1;
  pOption->maxShellConns = 1000;
  pOption->shellActivityTimer = 30;
  pOption->serverPort = 9527;
S
Shengliang Guan 已提交
48
  strcpy(pOption->dataDir, path);
S
Shengliang Guan 已提交
49 50 51
  strcpy(pOption->localEp, "localhost:9527");
  strcpy(pOption->localFqdn, "localhost");
  strcpy(pOption->firstEp, "localhost:9527");
S
Shengliang Guan 已提交
52 53 54

  taosRemoveDir(path);
  taosMkDir(path);
S
Shengliang Guan 已提交
55 56
}

S
Shengliang Guan 已提交
57
SServer* createServer(char *path) {
S
Shengliang Guan 已提交
58
  SDnodeOpt option = {0};
S
Shengliang Guan 已提交
59
  initOption(&option, path);
S
Shengliang Guan 已提交
60 61 62 63

  SDnode* pDnode = dndInit(&option);
  ASSERT(pDnode);

S
Shengliang Guan 已提交
64
  SServer* pServer = (SServer*)calloc(1, sizeof(SServer));
S
Shengliang Guan 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  ASSERT(pServer);

  pServer->pDnode = pDnode;
  pServer->threadId = taosCreateThread(runServer, pServer);
  ASSERT(pServer->threadId);

  return pServer;
}

void dropServer(SServer* pServer) {
  if (pServer->threadId != NULL) {
    taosDestoryThread(pServer->threadId);
  }
}

S
Shengliang Guan 已提交
80
void processClientRsp(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
S
Shengliang Guan 已提交
81
  SClient* pClient = (SClient*)parent;
S
Shengliang Guan 已提交
82
  pClient->pRsp = pMsg;
S
Shengliang Guan 已提交
83
  //taosMsleep(1000000);
S
Shengliang Guan 已提交
84
  tsem_post(&pClient->sem);
S
Shengliang Guan 已提交
85 86
}

S
Shengliang Guan 已提交
87
SClient* createClient(char *user, char *pass) {
S
Shengliang Guan 已提交
88
  SClient* pClient = (SClient*)calloc(1, sizeof(SClient));
S
Shengliang Guan 已提交
89 90
  ASSERT(pClient);

S
Shengliang Guan 已提交
91
  char secretEncrypt[32] = {0};
S
Shengliang Guan 已提交
92 93 94 95 96 97 98 99 100 101
  taosEncryptPass((uint8_t*)pass, strlen(pass), secretEncrypt);

  SRpcInit rpcInit;
  memset(&rpcInit, 0, sizeof(rpcInit));
  rpcInit.label = "DND-C";
  rpcInit.numOfThreads = 1;
  rpcInit.cfp = processClientRsp;
  rpcInit.sessions = 1024;
  rpcInit.connType = TAOS_CONN_CLIENT;
  rpcInit.idleTime = 30 * 1000;
S
Shengliang Guan 已提交
102
  rpcInit.user = user;
S
Shengliang Guan 已提交
103
  rpcInit.ckey = "key";
S
Shengliang Guan 已提交
104
  rpcInit.parent = pClient;
S
Shengliang Guan 已提交
105 106 107 108 109 110 111 112
  rpcInit.secret = (char*)secretEncrypt;
  rpcInit.parent = pClient;
  // rpcInit.spi = 1;

  pClient->clientRpc = rpcOpen(&rpcInit);
  ASSERT(pClient->clientRpc);

  tsem_init(&pClient->sem, 0, 0);
S
Shengliang Guan 已提交
113 114

  return pClient;
S
Shengliang Guan 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128
}

void dropClient(SClient* pClient) {
  tsem_destroy(&pClient->sem);
  rpcClose(pClient->clientRpc);
}

void sendMsg(SClient* pClient, SRpcMsg* pMsg) {
  SEpSet epSet = {0};
  epSet.inUse = 0;
  epSet.numOfEps = 1;
  epSet.port[0] = 9527;
  strcpy(epSet.fqdn[0], "localhost");

S
Shengliang Guan 已提交
129 130
  rpcSendRequest(pClient->clientRpc, &epSet, pMsg, NULL);
  tsem_wait(&pClient->sem);
S
Shengliang Guan 已提交
131
}