deploy.cpp 4.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

S
Shengliang Guan 已提交
18
void initLog(const char* path) {
19
  dDebugFlag = 207;
S
Shengliang Guan 已提交
20
  vDebugFlag = 0;
S
Shengliang Guan 已提交
21
  mDebugFlag = 207;
S
Shengliang Guan 已提交
22 23 24
  cDebugFlag = 0;
  jniDebugFlag = 0;
  tmrDebugFlag = 0;
S
Shengliang Guan 已提交
25
  uDebugFlag = 143;
S
Shengliang Guan 已提交
26 27 28 29 30 31 32
  rpcDebugFlag = 0;
  odbcDebugFlag = 0;
  qDebugFlag = 0;
  wDebugFlag = 0;
  sDebugFlag = 0;
  tsdbDebugFlag = 0;
  cqDebugFlag = 0;
S
Shengliang Guan 已提交
33
  tscEmbeddedInUtil = 1;
S
Shengliang Guan 已提交
34

S
Shengliang Guan 已提交
35
  taosRemoveDir(path);
S
Shengliang Guan 已提交
36 37
  taosMkDir(path);

S
Shengliang Guan 已提交
38 39 40 41 42 43
  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 已提交
44 45

void* runServer(void* param) {
S
Shengliang Guan 已提交
46
  SServer* pServer = (SServer*)param;
S
Shengliang Guan 已提交
47 48 49 50 51 52
  while (1) {
    taosMsleep(100);
    pthread_testcancel();
  }
}

53
void initOption(SDnodeOpt* pOption, const char* path, const char* fqdn, uint16_t port, const char* firstEp) {
S
Shengliang Guan 已提交
54 55 56 57 58 59 60 61 62 63
  pOption->sver = 1;
  pOption->numOfCores = 1;
  pOption->numOfSupportMnodes = 1;
  pOption->numOfSupportVnodes = 1;
  pOption->numOfSupportQnodes = 1;
  pOption->statusInterval = 1;
  pOption->numOfThreadsPerCore = 1;
  pOption->ratioOfQueryCores = 1;
  pOption->maxShellConns = 1000;
  pOption->shellActivityTimer = 30;
S
Shengliang Guan 已提交
64
  pOption->serverPort = port;
S
Shengliang Guan 已提交
65
  strcpy(pOption->dataDir, path);
S
Shengliang Guan 已提交
66
  snprintf(pOption->localEp, TSDB_EP_LEN, "%s:%u", fqdn, port);
S
Shengliang Guan 已提交
67
  snprintf(pOption->localFqdn, TSDB_FQDN_LEN, "%s", fqdn);
68
  snprintf(pOption->firstEp, TSDB_EP_LEN, "%s", firstEp);
S
Shengliang Guan 已提交
69
}
S
Shengliang Guan 已提交
70

S
Shengliang Guan 已提交
71
SServer* startServer(const char* path, const char* fqdn, uint16_t port, const char* firstEp) {
S
Shengliang Guan 已提交
72
  taosMkDir(path);
S
Shengliang Guan 已提交
73 74

  SDnodeOpt option = {0};
75
  initOption(&option, path, fqdn, port, firstEp);
S
Shengliang Guan 已提交
76 77 78 79

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

S
Shengliang Guan 已提交
80
  SServer* pServer = (SServer*)calloc(1, sizeof(SServer));
S
Shengliang Guan 已提交
81 82 83 84 85 86 87 88 89
  ASSERT(pServer);

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

  return pServer;
}

S
Shengliang Guan 已提交
90 91 92 93 94 95
SServer* createServer(const char* path, const char* fqdn, uint16_t port, const char* firstEp) {
  taosRemoveDir(path);
  return startServer(path, fqdn, port, firstEp);
}

void stopServer(SServer* pServer) {
96
  if (pServer == NULL) return;
S
Shengliang Guan 已提交
97 98 99
  if (pServer->threadId != NULL) {
    taosDestoryThread(pServer->threadId);
  }
S
Shengliang Guan 已提交
100 101 102 103 104

  if (pServer->pDnode != NULL) {
    dndCleanup(pServer->pDnode);
    pServer->pDnode = NULL;
  }
S
Shengliang Guan 已提交
105 106
}

S
Shengliang Guan 已提交
107
void processClientRsp(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
S
Shengliang Guan 已提交
108
  SClient* pClient = (SClient*)parent;
S
Shengliang Guan 已提交
109
  pClient->pRsp = pMsg;
110 111
  uInfo("response:%s from dnode, pCont:%p contLen:%d code:0x%X", taosMsg[pMsg->msgType], pMsg->pCont, pMsg->contLen,
        pMsg->code);
S
Shengliang Guan 已提交
112
  tsem_post(&pClient->sem);
S
Shengliang Guan 已提交
113 114
}

S
Shengliang Guan 已提交
115
SClient* createClient(const char* user, const char* pass, const char* fqdn, uint16_t port) {
S
Shengliang Guan 已提交
116
  SClient* pClient = (SClient*)calloc(1, sizeof(SClient));
S
Shengliang Guan 已提交
117 118
  ASSERT(pClient);

119
  char secretEncrypt[TSDB_PASSWORD_LEN] = {0};
S
Shengliang Guan 已提交
120 121 122 123
  taosEncryptPass((uint8_t*)pass, strlen(pass), secretEncrypt);

  SRpcInit rpcInit;
  memset(&rpcInit, 0, sizeof(rpcInit));
S
Shengliang Guan 已提交
124
  rpcInit.label = (char*)"DND-C";
S
Shengliang Guan 已提交
125 126 127 128 129
  rpcInit.numOfThreads = 1;
  rpcInit.cfp = processClientRsp;
  rpcInit.sessions = 1024;
  rpcInit.connType = TAOS_CONN_CLIENT;
  rpcInit.idleTime = 30 * 1000;
S
Shengliang Guan 已提交
130
  rpcInit.user = (char*)user;
S
Shengliang Guan 已提交
131
  rpcInit.ckey = (char*)"key";
S
Shengliang Guan 已提交
132
  rpcInit.parent = pClient;
S
Shengliang Guan 已提交
133 134 135 136 137 138 139 140
  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 已提交
141 142
  strcpy(pClient->fqdn, fqdn);
  pClient->port = port;
S
Shengliang Guan 已提交
143 144

  return pClient;
S
Shengliang Guan 已提交
145 146 147 148 149 150 151 152 153 154 155
}

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;
S
Shengliang Guan 已提交
156
  epSet.port[0] = pClient->port;
157
  memcpy(epSet.fqdn[0], pClient->fqdn, TSDB_FQDN_LEN);
S
Shengliang Guan 已提交
158

S
Shengliang Guan 已提交
159 160
  rpcSendRequest(pClient->clientRpc, &epSet, pMsg, NULL);
  tsem_wait(&pClient->sem);
S
Shengliang Guan 已提交
161
}