vmFile.c 6.6 KB
Newer Older
S
shm  
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 "vmInt.h"
S
shm  
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19 20
#define MAX_CONTENT_LEN 1024 * 1024

S
Shengliang 已提交
21
SVnodeObj **vmGetVnodeListFromHash(SVnodeMgmt *pMgmt, int32_t *numOfVnodes) {
22
  taosThreadRwlockRdlock(&pMgmt->lock);
S
shm  
Shengliang Guan 已提交
23 24 25

  int32_t     num = 0;
  int32_t     size = taosHashGetSize(pMgmt->hash);
wafwerar's avatar
wafwerar 已提交
26
  SVnodeObj **pVnodes = taosMemoryCalloc(size, sizeof(SVnodeObj *));
S
shm  
Shengliang Guan 已提交
27 28 29 30 31 32 33

  void *pIter = taosHashIterate(pMgmt->hash, NULL);
  while (pIter) {
    SVnodeObj **ppVnode = pIter;
    SVnodeObj  *pVnode = *ppVnode;
    if (pVnode && num < size) {
      int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1);
34
      // dTrace("vgId:%d, acquire vnode list, ref:%d", pVnode->vgId, refCount);
S
Shengliang Guan 已提交
35
      pVnodes[num++] = (*ppVnode);
S
shm  
Shengliang Guan 已提交
36 37 38 39 40 41
      pIter = taosHashIterate(pMgmt->hash, pIter);
    } else {
      taosHashCancelIterate(pMgmt->hash, pIter);
    }
  }

42
  taosThreadRwlockUnlock(&pMgmt->lock);
S
shm  
Shengliang Guan 已提交
43 44 45 46 47
  *numOfVnodes = num;

  return pVnodes;
}

S
Shengliang 已提交
48
int32_t vmGetVnodeListFromFile(SVnodeMgmt *pMgmt, SWrapperCfg **ppCfgs, int32_t *numOfVnodes) {
49
  int32_t      code = TSDB_CODE_INVALID_JSON_FORMAT;
S
shm  
Shengliang Guan 已提交
50
  int32_t      len = 0;
S
Shengliang Guan 已提交
51
  int32_t      maxLen = MAX_CONTENT_LEN;
wafwerar's avatar
wafwerar 已提交
52
  char        *content = taosMemoryCalloc(1, maxLen + 1);
S
shm  
Shengliang Guan 已提交
53 54
  cJSON       *root = NULL;
  FILE        *fp = NULL;
S
Shengliang Guan 已提交
55
  char         file[PATH_MAX] = {0};
S
shm  
Shengliang Guan 已提交
56 57 58 59 60 61 62 63 64
  SWrapperCfg *pCfgs = NULL;
  TdFilePtr    pFile = NULL;

  snprintf(file, sizeof(file), "%s%svnodes.json", pMgmt->path, TD_DIRSEP);

  pFile = taosOpenFile(file, TD_FILE_READ);
  if (pFile == NULL) {
    dDebug("file %s not exist", file);
    code = 0;
S
Shengliang Guan 已提交
65
    goto _OVER;
S
shm  
Shengliang Guan 已提交
66 67
  }

68 69 70 71 72
  if (content == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
shm  
Shengliang Guan 已提交
73 74 75
  len = (int32_t)taosReadFile(pFile, content, maxLen);
  if (len <= 0) {
    dError("failed to read %s since content is null", file);
S
Shengliang Guan 已提交
76
    goto _OVER;
S
shm  
Shengliang Guan 已提交
77 78 79 80 81 82
  }

  content[len] = 0;
  root = cJSON_Parse(content);
  if (root == NULL) {
    dError("failed to read %s since invalid json format", file);
S
Shengliang Guan 已提交
83
    goto _OVER;
S
shm  
Shengliang Guan 已提交
84 85 86 87 88
  }

  cJSON *vnodes = cJSON_GetObjectItem(root, "vnodes");
  if (!vnodes || vnodes->type != cJSON_Array) {
    dError("failed to read %s since vnodes not found", file);
S
Shengliang Guan 已提交
89
    goto _OVER;
S
shm  
Shengliang Guan 已提交
90 91 92 93
  }

  int32_t vnodesNum = cJSON_GetArraySize(vnodes);
  if (vnodesNum > 0) {
wafwerar's avatar
wafwerar 已提交
94
    pCfgs = taosMemoryCalloc(vnodesNum, sizeof(SWrapperCfg));
S
shm  
Shengliang Guan 已提交
95 96
    if (pCfgs == NULL) {
      dError("failed to read %s since out of memory", file);
S
Shengliang Guan 已提交
97
      code = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
98
      goto _OVER;
S
shm  
Shengliang Guan 已提交
99 100 101 102 103 104 105 106 107
    }

    for (int32_t i = 0; i < vnodesNum; ++i) {
      cJSON       *vnode = cJSON_GetArrayItem(vnodes, i);
      SWrapperCfg *pCfg = &pCfgs[i];

      cJSON *vgId = cJSON_GetObjectItem(vnode, "vgId");
      if (!vgId || vgId->type != cJSON_Number) {
        dError("failed to read %s since vgId not found", file);
S
Shengliang Guan 已提交
108
        taosMemoryFree(pCfgs);
S
Shengliang Guan 已提交
109
        goto _OVER;
S
shm  
Shengliang Guan 已提交
110 111 112 113 114 115 116
      }
      pCfg->vgId = vgId->valueint;
      snprintf(pCfg->path, sizeof(pCfg->path), "%s%svnode%d", pMgmt->path, TD_DIRSEP, pCfg->vgId);

      cJSON *dropped = cJSON_GetObjectItem(vnode, "dropped");
      if (!dropped || dropped->type != cJSON_Number) {
        dError("failed to read %s since dropped not found", file);
S
Shengliang Guan 已提交
117
        taosMemoryFree(pCfgs);
S
Shengliang Guan 已提交
118
        goto _OVER;
S
shm  
Shengliang Guan 已提交
119 120 121 122 123 124
      }
      pCfg->dropped = dropped->valueint;

      cJSON *vgVersion = cJSON_GetObjectItem(vnode, "vgVersion");
      if (!vgVersion || vgVersion->type != cJSON_Number) {
        dError("failed to read %s since vgVersion not found", file);
S
Shengliang Guan 已提交
125
        taosMemoryFree(pCfgs);
S
Shengliang Guan 已提交
126
        goto _OVER;
S
shm  
Shengliang Guan 已提交
127 128 129 130 131 132 133 134 135
      }
      pCfg->vgVersion = vgVersion->valueint;
    }

    *ppCfgs = pCfgs;
  }

  *numOfVnodes = vnodesNum;
  code = 0;
S
Shengliang Guan 已提交
136
  dDebug("succcessed to read file %s, numOfVnodes:%d", file, vnodesNum);
S
shm  
Shengliang Guan 已提交
137

S
Shengliang Guan 已提交
138
_OVER:
wafwerar's avatar
wafwerar 已提交
139
  if (content != NULL) taosMemoryFree(content);
S
shm  
Shengliang Guan 已提交
140 141 142 143 144 145 146
  if (root != NULL) cJSON_Delete(root);
  if (pFile != NULL) taosCloseFile(&pFile);

  terrno = code;
  return code;
}

S
Shengliang 已提交
147
int32_t vmWriteVnodeListToFile(SVnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
148
  int32_t code = 0;
S
Shengliang Guan 已提交
149 150
  char    file[PATH_MAX] = {0};
  char    realfile[PATH_MAX] = {0};
S
shm  
Shengliang Guan 已提交
151 152 153
  snprintf(file, sizeof(file), "%s%svnodes.json.bak", pMgmt->path, TD_DIRSEP);
  snprintf(realfile, sizeof(file), "%s%svnodes.json", pMgmt->path, TD_DIRSEP);

154
  TdFilePtr pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
S
shm  
Shengliang Guan 已提交
155 156 157 158 159 160 161
  if (pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to write %s since %s", file, terrstr());
    return -1;
  }

  int32_t     numOfVnodes = 0;
S
Shengliang Guan 已提交
162 163
  SVnodeObj **ppVnodes = vmGetVnodeListFromHash(pMgmt, &numOfVnodes);
  if (ppVnodes == NULL) {
S
Shengliang Guan 已提交
164
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
165
    code = -1;
S
Shengliang Guan 已提交
166 167
    goto _OVER;
  }
S
shm  
Shengliang Guan 已提交
168 169

  int32_t len = 0;
S
Shengliang Guan 已提交
170
  int32_t maxLen = MAX_CONTENT_LEN;
wafwerar's avatar
wafwerar 已提交
171
  char   *content = taosMemoryCalloc(1, maxLen + 1);
S
Shengliang Guan 已提交
172
  if (content == NULL) {
173
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
174
    code = -1;
S
Shengliang Guan 已提交
175
    goto _OVER;
176
  }
S
shm  
Shengliang Guan 已提交
177 178 179 180

  len += snprintf(content + len, maxLen - len, "{\n");
  len += snprintf(content + len, maxLen - len, "  \"vnodes\": [\n");
  for (int32_t i = 0; i < numOfVnodes; ++i) {
S
Shengliang Guan 已提交
181
    SVnodeObj *pVnode = ppVnodes[i];
S
Shengliang Guan 已提交
182 183
    if (pVnode == NULL) continue;

S
shm  
Shengliang Guan 已提交
184 185 186
    len += snprintf(content + len, maxLen - len, "    {\n");
    len += snprintf(content + len, maxLen - len, "      \"vgId\": %d,\n", pVnode->vgId);
    len += snprintf(content + len, maxLen - len, "      \"dropped\": %d,\n", pVnode->dropped);
187
    len += snprintf(content + len, maxLen - len, "      \"vgVersion\": %d\n", pVnode->vgVersion);
S
shm  
Shengliang Guan 已提交
188 189 190 191 192 193 194 195
    if (i < numOfVnodes - 1) {
      len += snprintf(content + len, maxLen - len, "    },\n");
    } else {
      len += snprintf(content + len, maxLen - len, "    }\n");
    }
  }
  len += snprintf(content + len, maxLen - len, "  ]\n");
  len += snprintf(content + len, maxLen - len, "}\n");
S
Shengliang Guan 已提交
196 197 198
  terrno = 0;

_OVER:
S
shm  
Shengliang Guan 已提交
199 200 201
  taosWriteFile(pFile, content, len);
  taosFsyncFile(pFile);
  taosCloseFile(&pFile);
wafwerar's avatar
wafwerar 已提交
202
  taosMemoryFree(content);
S
shm  
Shengliang Guan 已提交
203

S
Shengliang Guan 已提交
204
  if (ppVnodes != NULL) {
S
Shengliang Guan 已提交
205 206 207 208 209 210
    for (int32_t i = 0; i < numOfVnodes; ++i) {
      SVnodeObj *pVnode = ppVnodes[i];
      if (pVnode != NULL) {
        vmReleaseVnode(pMgmt, pVnode);
      }
    }
S
Shengliang Guan 已提交
211
    taosMemoryFree(ppVnodes);
S
shm  
Shengliang Guan 已提交
212 213
  }

S
Shengliang Guan 已提交
214
  if (code != 0) return -1;
S
Shengliang Guan 已提交
215

S
Shengliang Guan 已提交
216
  dDebug("successed to write %s, numOfVnodes:%d", realfile, numOfVnodes);
S
shm  
Shengliang Guan 已提交
217 218
  return taosRenameFile(file, realfile);
}