smWorker.c 6.0 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
S
shm  
Shengliang Guan 已提交
17
#include "smInt.h"
S
Shengliang Guan 已提交
18

L
Liu Jicong 已提交
19 20 21 22

static void dndProcessSnodeSharedQueue(SDnode *pDnode, SRpcMsg *pMsg);

static void dndProcessSnodeUniqueQueue(SDnode *pDnode, STaosQall *qall, int32_t numOfMsgs);
S
Shengliang Guan 已提交
23 24 25

static int32_t dndStartSnodeWorker(SDnode *pDnode) {
  SSnodeMgmt *pMgmt = &pDnode->smgmt;
L
Liu Jicong 已提交
26
  pMgmt->uniqueWorkers = taosArrayInit(0, sizeof(void *));
L
Liu Jicong 已提交
27
  for (int32_t i = 0; i < SND_UNIQUE_THREAD_NUM; i++) {
L
Liu Jicong 已提交
28 29 30 31 32
    SDnodeWorker *pUniqueWorker = malloc(sizeof(SDnodeWorker));
    if (pUniqueWorker == NULL) {
      return -1;
    }
    if (dndInitWorker(pDnode, pUniqueWorker, DND_WORKER_MULTI, "snode-unique", 1, 1, dndProcessSnodeSharedQueue) != 0) {
L
Liu Jicong 已提交
33 34 35
      dError("failed to start snode unique worker since %s", terrstr());
      return -1;
    }
L
Liu Jicong 已提交
36
    taosArrayPush(pMgmt->uniqueWorkers, &pUniqueWorker);
L
Liu Jicong 已提交
37
  }
L
Liu Jicong 已提交
38 39
  if (dndInitWorker(pDnode, &pMgmt->sharedWorker, DND_WORKER_SINGLE, "snode-shared", SND_SHARED_THREAD_NUM,
                    SND_SHARED_THREAD_NUM, dndProcessSnodeSharedQueue)) {
L
Liu Jicong 已提交
40
    dError("failed to start snode shared worker since %s", terrstr());
S
Shengliang Guan 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53
    return -1;
  }

  return 0;
}

static void dndStopSnodeWorker(SDnode *pDnode) {
  SSnodeMgmt *pMgmt = &pDnode->smgmt;

  taosWLockLatch(&pMgmt->latch);
  pMgmt->deployed = 0;
  taosWUnLockLatch(&pMgmt->latch);

S
Shengliang Guan 已提交
54
  while (pMgmt->refCount > 0) {
S
Shengliang Guan 已提交
55
    taosMsleep(10);
L
Liu Jicong 已提交
56
  }
S
Shengliang Guan 已提交
57

L
Liu Jicong 已提交
58 59 60 61 62
  for (int32_t i = 0; i < taosArrayGetSize(pMgmt->uniqueWorkers); i++) {
    SDnodeWorker *worker = taosArrayGetP(pMgmt->uniqueWorkers, i);
    dndCleanupWorker(worker);
  }
  taosArrayDestroy(pMgmt->uniqueWorkers);
S
Shengliang Guan 已提交
63 64
}

L
Liu Jicong 已提交
65
static void dndProcessSnodeUniqueQueue(SDnode *pDnode, STaosQall *qall, int32_t numOfMsgs) {
L
Liu Jicong 已提交
66
  /*SSnodeMgmt *pMgmt = &pDnode->smgmt;*/
S
shm  
Shengliang Guan 已提交
67
  int32_t code = TSDB_CODE_NODE_NOT_DEPLOYED;
S
Shengliang Guan 已提交
68 69 70

  SSnode *pSnode = dndAcquireSnode(pDnode);
  if (pSnode != NULL) {
L
Liu Jicong 已提交
71 72 73 74 75 76
    for (int32_t i = 0; i < numOfMsgs; i++) {
      SRpcMsg *pMsg = NULL;
      taosGetQitem(qall, (void **)&pMsg);

      sndProcessUMsg(pSnode, pMsg);

L
Liu Jicong 已提交
77 78 79 80 81 82 83 84 85 86 87
      rpcFreeCont(pMsg->pCont);
      taosFreeQitem(pMsg);
    }
    dndReleaseSnode(pDnode, pSnode);
  } else {
    for (int32_t i = 0; i < numOfMsgs; i++) {
      SRpcMsg *pMsg = NULL;
      taosGetQitem(qall, (void **)&pMsg);
      SRpcMsg rpcRsp = {.handle = pMsg->handle, .ahandle = pMsg->ahandle, .code = code};
      rpcSendResponse(&rpcRsp);

L
Liu Jicong 已提交
88 89 90
      rpcFreeCont(pMsg->pCont);
      taosFreeQitem(pMsg);
    }
S
Shengliang Guan 已提交
91
  }
L
Liu Jicong 已提交
92
}
S
Shengliang Guan 已提交
93

L
Liu Jicong 已提交
94
static void dndProcessSnodeSharedQueue(SDnode *pDnode, SRpcMsg *pMsg) {
L
Liu Jicong 已提交
95
  /*SSnodeMgmt *pMgmt = &pDnode->smgmt;*/
S
shm  
Shengliang Guan 已提交
96
  int32_t code = TSDB_CODE_NODE_NOT_DEPLOYED;
L
Liu Jicong 已提交
97 98 99

  SSnode *pSnode = dndAcquireSnode(pDnode);
  if (pSnode != NULL) {
L
Liu Jicong 已提交
100 101 102 103 104
    sndProcessSMsg(pSnode, pMsg);
    dndReleaseSnode(pDnode, pSnode);
  } else {
    SRpcMsg rpcRsp = {.handle = pMsg->handle, .ahandle = pMsg->ahandle, .code = code};
    rpcSendResponse(&rpcRsp);
L
Liu Jicong 已提交
105 106 107
  }

#if 0
108 109 110 111 112 113 114 115 116 117
  if (pMsg->msgType & 1u) {
    if (pRsp != NULL) {
      pRsp->ahandle = pMsg->ahandle;
      rpcSendResponse(pRsp);
      free(pRsp);
    } else {
      if (code != 0) code = terrno;
      SRpcMsg rpcRsp = {.handle = pMsg->handle, .ahandle = pMsg->ahandle, .code = code};
      rpcSendResponse(&rpcRsp);
    }
S
Shengliang Guan 已提交
118
  }
L
Liu Jicong 已提交
119
#endif
S
Shengliang Guan 已提交
120 121 122 123 124

  rpcFreeCont(pMsg->pCont);
  taosFreeQitem(pMsg);
}

L
Liu Jicong 已提交
125 126 127 128 129 130 131
static FORCE_INLINE int32_t dndGetSWIdFromMsg(SRpcMsg *pMsg) {
  SMsgHead *pHead = pMsg->pCont;
  pHead->streamTaskId = htonl(pHead->streamTaskId);
  return pHead->streamTaskId % SND_UNIQUE_THREAD_NUM;
}

static void dndWriteSnodeMsgToWorkerByMsg(SDnode *pDnode, SRpcMsg *pMsg) {
S
shm  
Shengliang Guan 已提交
132
  int32_t code = TSDB_CODE_NODE_NOT_DEPLOYED;
L
Liu Jicong 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

  SSnode *pSnode = dndAcquireSnode(pDnode);
  if (pSnode != NULL) {
    int32_t       index = dndGetSWIdFromMsg(pMsg);
    SDnodeWorker *pWorker = taosArrayGetP(pDnode->smgmt.uniqueWorkers, index);
    code = dndWriteMsgToWorker(pWorker, pMsg, sizeof(SRpcMsg));
  }

  dndReleaseSnode(pDnode, pSnode);

  if (code != 0) {
    if (pMsg->msgType & 1u) {
      SRpcMsg rsp = {.handle = pMsg->handle, .ahandle = pMsg->ahandle, .code = code};
      rpcSendResponse(&rsp);
    }
    rpcFreeCont(pMsg->pCont);
  }
}

static void dndWriteSnodeMsgToMgmtWorker(SDnode *pDnode, SRpcMsg *pMsg) {
S
shm  
Shengliang Guan 已提交
153
  int32_t code = TSDB_CODE_NODE_NOT_DEPLOYED;
L
Liu Jicong 已提交
154 155 156

  SSnode *pSnode = dndAcquireSnode(pDnode);
  if (pSnode != NULL) {
L
Liu Jicong 已提交
157
    SDnodeWorker *pWorker = taosArrayGet(pDnode->smgmt.uniqueWorkers, 0);
L
Liu Jicong 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170
    code = dndWriteMsgToWorker(pWorker, pMsg, sizeof(SRpcMsg));
  }
  dndReleaseSnode(pDnode, pSnode);

  if (code != 0) {
    if (pMsg->msgType & 1u) {
      SRpcMsg rsp = {.handle = pMsg->handle, .ahandle = pMsg->ahandle, .code = code};
      rpcSendResponse(&rsp);
    }
    rpcFreeCont(pMsg->pCont);
  }
}

S
Shengliang Guan 已提交
171
static void dndWriteSnodeMsgToWorker(SDnode *pDnode, SDnodeWorker *pWorker, SRpcMsg *pMsg) {
S
shm  
Shengliang Guan 已提交
172
  int32_t code = TSDB_CODE_NODE_NOT_DEPLOYED;
S
Shengliang Guan 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

  SSnode *pSnode = dndAcquireSnode(pDnode);
  if (pSnode != NULL) {
    code = dndWriteMsgToWorker(pWorker, pMsg, sizeof(SRpcMsg));
  }
  dndReleaseSnode(pDnode, pSnode);

  if (code != 0) {
    if (pMsg->msgType & 1u) {
      SRpcMsg rsp = {.handle = pMsg->handle, .ahandle = pMsg->ahandle, .code = code};
      rpcSendResponse(&rsp);
    }
    rpcFreeCont(pMsg->pCont);
  }
}

L
Liu Jicong 已提交
189 190 191 192
void dndProcessSnodeMgmtMsg(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) {
  dndWriteSnodeMsgToMgmtWorker(pDnode, pMsg);
}

L
Liu Jicong 已提交
193
void dndProcessSnodeUniqueMsg(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) {
L
Liu Jicong 已提交
194
  dndWriteSnodeMsgToWorkerByMsg(pDnode, pMsg);
L
Liu Jicong 已提交
195 196 197 198
}

void dndProcessSnodeSharedMsg(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) {
  dndWriteSnodeMsgToWorker(pDnode, &pDnode->smgmt.sharedWorker, pMsg);
S
Shengliang Guan 已提交
199
}