sdbFile.c 5.6 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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
#include "sdbInt.h"
S
Shengliang Guan 已提交
18
#include "tchecksum.h"
S
Shengliang Guan 已提交
19

S
Shengliang Guan 已提交
20
static int32_t sdbRunDeployFp(SSdb *pSdb) {
S
Shengliang Guan 已提交
21
  mDebug("start to deploy sdb");
S
Shengliang Guan 已提交
22

23
  for (ESdbType i = SDB_MAX - 1; i > SDB_START; --i) {
S
Shengliang Guan 已提交
24
    SdbDeployFp fp = pSdb->deployFps[i];
S
Shengliang Guan 已提交
25
    if (fp == NULL) continue;
S
Shengliang Guan 已提交
26

S
Shengliang Guan 已提交
27
    if ((*fp)(pSdb->pMnode) != 0) {
S
Shengliang Guan 已提交
28 29
      mError("failed to deploy sdb:%d since %s", i, terrstr());
      return -1;
S
Shengliang Guan 已提交
30 31 32
    }
  }

S
Shengliang Guan 已提交
33
  mDebug("sdb deploy successfully");
S
Shengliang Guan 已提交
34 35 36
  return 0;
}

S
Shengliang Guan 已提交
37 38 39 40 41 42
int32_t sdbReadFile(SSdb *pSdb) {
  int64_t offset = 0;
  int32_t code = 0;
  int32_t readLen = 0;
  int64_t ret = 0;

S
Shengliang Guan 已提交
43 44
  SSdbRaw *pRaw = malloc(SDB_MAX_SIZE);
  if (pRaw == NULL) {
S
Shengliang Guan 已提交
45 46 47
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    mError("failed read file since %s",  terrstr());
    return -1;
S
Shengliang Guan 已提交
48 49 50
  }

  char file[PATH_MAX] = {0};
S
Shengliang Guan 已提交
51 52
  snprintf(file, sizeof(file), "%s%ssdb.data", pSdb->currDir, TD_DIRSEP);

S
Shengliang Guan 已提交
53
  FileFd fd = taosOpenFileRead(file);
S
Shengliang Guan 已提交
54
  if (fd <= 0) {
S
Shengliang Guan 已提交
55
    free(pRaw);
S
Shengliang Guan 已提交
56 57
    terrno = TAOS_SYSTEM_ERROR(errno);
    mError("failed to read file:%s since %s", file, terrstr());
S
Shengliang Guan 已提交
58
    return 0;
S
Shengliang Guan 已提交
59 60 61
  }

  while (1) {
S
Shengliang Guan 已提交
62 63
    readLen = sizeof(SSdbRaw);
    ret = taosReadFile(fd, pRaw, readLen);
S
Shengliang Guan 已提交
64 65 66 67 68 69 70 71
    if (ret == 0) break;

    if (ret < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      mError("failed to read file:%s since %s", file, tstrerror(code));
      break;
    }

S
Shengliang Guan 已提交
72
    if (ret != readLen) {
S
Shengliang Guan 已提交
73 74 75 76 77
      code = TSDB_CODE_FILE_CORRUPTED;
      mError("failed to read file:%s since %s", file, tstrerror(code));
      break;
    }

S
Shengliang Guan 已提交
78 79
    readLen = pRaw->dataLen + sizeof(int32_t);
    ret = taosReadFile(fd, pRaw->pData, readLen);
S
Shengliang Guan 已提交
80 81 82 83 84 85
    if (ret < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      mError("failed to read file:%s since %s", file, tstrerror(code));
      break;
    }

S
Shengliang Guan 已提交
86
    if (ret != readLen) {
S
Shengliang Guan 已提交
87 88 89 90 91
      code = TSDB_CODE_FILE_CORRUPTED;
      mError("failed to read file:%s since %s", file, tstrerror(code));
      break;
    }

S
Shengliang Guan 已提交
92 93
    int32_t totalLen = sizeof(SSdbRaw) + pRaw->dataLen + sizeof(int32_t);
    if (!taosCheckChecksumWhole((const uint8_t *)pRaw, totalLen) != 0) {
S
Shengliang Guan 已提交
94
      code = TSDB_CODE_CHECKSUM_ERROR;
S
Shengliang Guan 已提交
95 96 97 98
      mError("failed to read file:%s since %s", file, tstrerror(code));
      break;
    }

S
Shengliang Guan 已提交
99
    code = sdbWriteNotFree(pSdb, pRaw);
S
Shengliang Guan 已提交
100
    if (code != 0) {
S
Shengliang Guan 已提交
101
      mError("failed to read file:%s since %s", file, terrstr());
S
Shengliang Guan 已提交
102 103 104 105 106 107 108 109
      goto PARSE_SDB_DATA_ERROR;
    }
  }

  code = 0;

PARSE_SDB_DATA_ERROR:
  taosCloseFile(fd);
S
Shengliang Guan 已提交
110
  sdbFreeRaw(pRaw);
S
Shengliang Guan 已提交
111

S
Shengliang Guan 已提交
112
  terrno = code;
S
Shengliang Guan 已提交
113 114 115
  return code;
}

S
Shengliang Guan 已提交
116 117 118
int32_t sdbWriteFile(SSdb *pSdb) {
  int32_t code = 0;

S
Shengliang Guan 已提交
119
  char tmpfile[PATH_MAX] = {0};
S
Shengliang Guan 已提交
120
  snprintf(tmpfile, sizeof(tmpfile), "%s%ssdb.data", pSdb->tmpDir, TD_DIRSEP);
S
Shengliang Guan 已提交
121
  char curfile[PATH_MAX] = {0};
S
Shengliang Guan 已提交
122
  snprintf(curfile, sizeof(curfile), "%s%ssdb.data", pSdb->currDir, TD_DIRSEP);
S
Shengliang Guan 已提交
123 124 125

  FileFd fd = taosOpenFileCreateWrite(tmpfile);
  if (fd <= 0) {
S
Shengliang Guan 已提交
126 127 128
    terrno = TAOS_SYSTEM_ERROR(errno);
    mError("failed to open file:%s for write since %s", tmpfile, terrstr());
    return -1;
S
Shengliang Guan 已提交
129 130
  }

131
  for (ESdbType i = SDB_MAX - 1; i > SDB_START; --i) {
S
Shengliang Guan 已提交
132
    SdbEncodeFp encodeFp = pSdb->encodeFps[i];
S
Shengliang Guan 已提交
133 134
    if (encodeFp == NULL) continue;

S
Shengliang Guan 已提交
135 136
    SHashObj *hash = pSdb->hashObjs[i];
    SRWLatch *pLock = &pSdb->locks[i];
S
Shengliang Guan 已提交
137
    taosWLockLatch(pLock);
S
Shengliang Guan 已提交
138

S
Shengliang Guan 已提交
139 140 141 142 143 144 145
    SSdbRow **ppRow = taosHashIterate(hash, NULL);
    while (ppRow != NULL) {
      SSdbRow *pRow = *ppRow;
      if (pRow == NULL || pRow->status != SDB_STATUS_READY) {
        ppRow = taosHashIterate(hash, ppRow);
        continue;
      }
S
Shengliang Guan 已提交
146

S
Shengliang Guan 已提交
147 148
      SSdbRaw *pRaw = (*encodeFp)(pRow->pObj);
      if (pRaw != NULL) {
S
Shengliang Guan 已提交
149
        pRaw->status = pRow->status;
S
Shengliang Guan 已提交
150 151 152
        int32_t writeLen = sizeof(SSdbRaw) + pRaw->dataLen;
        if (taosWriteFile(fd, pRaw, writeLen) != writeLen) {
          code = TAOS_SYSTEM_ERROR(terrno);
S
Shengliang Guan 已提交
153
          taosHashCancelIterate(hash, ppRow);
154
          free(pRaw);
S
Shengliang Guan 已提交
155 156
          break;
        }
S
Shengliang Guan 已提交
157 158

        int32_t cksum = taosCalcChecksum(0, (const uint8_t *)pRaw, sizeof(SSdbRaw) + pRaw->dataLen);
S
Shengliang Guan 已提交
159 160
        if (taosWriteFile(fd, &cksum, sizeof(int32_t)) != sizeof(int32_t)) {
          code = TAOS_SYSTEM_ERROR(terrno);
S
Shengliang Guan 已提交
161
          taosHashCancelIterate(hash, ppRow);
162
          free(pRaw);
S
Shengliang Guan 已提交
163 164
          break;
        }
S
Shengliang Guan 已提交
165 166
      } else {
        code = TSDB_CODE_SDB_APP_ERROR;
S
Shengliang Guan 已提交
167
        taosHashCancelIterate(hash, ppRow);
S
Shengliang Guan 已提交
168 169 170
        break;
      }

171
      free(pRaw);
S
Shengliang Guan 已提交
172
      ppRow = taosHashIterate(hash, ppRow);
S
Shengliang Guan 已提交
173
    }
S
Shengliang Guan 已提交
174
    taosWUnLockLatch(pLock);
S
Shengliang Guan 已提交
175 176 177 178
  }

  if (code == 0) {
    code = taosFsyncFile(fd);
S
Shengliang Guan 已提交
179 180 181 182
    if (code != 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      mError("failed to write file:%s since %s", tmpfile, tstrerror(code));
    }
S
Shengliang Guan 已提交
183 184
  }

S
Shengliang Guan 已提交
185 186
  taosCloseFile(fd);

S
Shengliang Guan 已提交
187
  if (code == 0) {
S
Shengliang Guan 已提交
188
    code = taosRenameFile(tmpfile, curfile);
S
Shengliang Guan 已提交
189 190 191 192
    if (code != 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      mError("failed to write file:%s since %s", curfile, tstrerror(code));
    }
S
Shengliang Guan 已提交
193 194 195
  }

  if (code != 0) {
S
Shengliang Guan 已提交
196
    mError("failed to write file:%s since %s", curfile, tstrerror(code));
S
Shengliang Guan 已提交
197
  } else {
S
Shengliang Guan 已提交
198
    mDebug("write file:%s successfully", curfile);
S
Shengliang Guan 已提交
199 200
  }

S
Shengliang Guan 已提交
201
  terrno = code;
S
Shengliang Guan 已提交
202 203 204
  return code;
}

S
Shengliang Guan 已提交
205
int32_t sdbDeploy(SSdb *pSdb) {
S
Shengliang Guan 已提交
206 207
  if (sdbRunDeployFp(pSdb) != 0) {
    return -1;
S
Shengliang Guan 已提交
208 209
  }

S
Shengliang Guan 已提交
210 211
  if (sdbWriteFile(pSdb) != 0) {
    return -1;
S
Shengliang Guan 已提交
212 213 214 215
  }

  return 0;
}