vnodeSnapshot.c 8.3 KB
Newer Older
H
Hongze Cheng 已提交
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/>.
 */

H
Hongze Cheng 已提交
16
#include "vnd.h"
H
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18
// SVSnapReader ========================================================
H
Hongze Cheng 已提交
19
struct SVSnapReader {
H
Hongze Cheng 已提交
20 21 22
  SVnode *pVnode;
  int64_t sver;
  int64_t ever;
H
Hongze Cheng 已提交
23
  int64_t index;
H
Hongze Cheng 已提交
24 25
  // meta
  int8_t           metaDone;
H
Hongze Cheng 已提交
26
  SMetaSnapReader *pMetaReader;
H
Hongze Cheng 已提交
27 28
  // tsdb
  int8_t           tsdbDone;
H
Hongze Cheng 已提交
29
  STsdbSnapReader *pTsdbReader;
C
Cary Xu 已提交
30 31
  // rsma
  int8_t rsmaDone[TSDB_RETENTION_L2];
H
Hongze Cheng 已提交
32 33
};

H
Hongze Cheng 已提交
34
int32_t vnodeSnapReaderOpen(SVnode *pVnode, int64_t sver, int64_t ever, SVSnapReader **ppReader) {
H
Hongze Cheng 已提交
35
  int32_t       code = 0;
H
Hongze Cheng 已提交
36
  SVSnapReader *pReader = NULL;
H
Hongze Cheng 已提交
37

H
Hongze Cheng 已提交
38
  pReader = (SVSnapReader *)taosMemoryCalloc(1, sizeof(*pReader));
H
Hongze Cheng 已提交
39 40 41 42 43 44 45 46
  if (pReader == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pReader->pVnode = pVnode;
  pReader->sver = sver;
  pReader->ever = ever;

H
Hongze Cheng 已提交
47
  vInfo("vgId:%d vnode snapshot reader opened, sver:%" PRId64 " ever:%" PRId64, TD_VID(pVnode), sver, ever);
H
Hongze Cheng 已提交
48
  *ppReader = pReader;
H
Hongze Cheng 已提交
49
  return code;
H
Hongze Cheng 已提交
50 51

_err:
H
Hongze Cheng 已提交
52
  vError("vgId:%d vnode snapshot reader open failed since %s", TD_VID(pVnode), tstrerror(code));
H
Hongze Cheng 已提交
53
  *ppReader = NULL;
H
Hongze Cheng 已提交
54
  return code;
H
Hongze Cheng 已提交
55 56
}

H
Hongze Cheng 已提交
57
int32_t vnodeSnapReaderClose(SVSnapReader *pReader) {
H
Hongze Cheng 已提交
58 59
  int32_t code = 0;

H
Hongze Cheng 已提交
60 61 62 63 64 65 66
  if (pReader->pTsdbReader) {
    tsdbSnapReaderClose(&pReader->pTsdbReader);
  }

  if (pReader->pMetaReader) {
    metaSnapReaderClose(&pReader->pMetaReader);
  }
H
Hongze Cheng 已提交
67

H
Hongze Cheng 已提交
68
  vInfo("vgId:%d vnode snapshot reader closed", TD_VID(pReader->pVnode));
H
Hongze Cheng 已提交
69
  taosMemoryFree(pReader);
H
Hongze Cheng 已提交
70
  return code;
H
Hongze Cheng 已提交
71 72
}

H
Hongze Cheng 已提交
73
int32_t vnodeSnapRead(SVSnapReader *pReader, uint8_t **ppData, uint32_t *nData) {
H
Hongze Cheng 已提交
74 75
  int32_t code = 0;

H
Hongze Cheng 已提交
76
  // META ==============
H
Hongze Cheng 已提交
77
  if (!pReader->metaDone) {
H
Hongze Cheng 已提交
78 79 80 81 82 83 84
    // open reader if not
    if (pReader->pMetaReader == NULL) {
      code = metaSnapReaderOpen(pReader->pVnode->pMeta, pReader->sver, pReader->ever, &pReader->pMetaReader);
      if (code) goto _err;
    }

    code = metaSnapRead(pReader->pMetaReader, ppData);
H
Hongze Cheng 已提交
85
    if (code) {
H
Hongze Cheng 已提交
86 87 88 89
      goto _err;
    } else {
      if (*ppData) {
        goto _exit;
H
Hongze Cheng 已提交
90
      } else {
H
Hongze Cheng 已提交
91 92 93
        pReader->metaDone = 1;
        code = metaSnapReaderClose(&pReader->pMetaReader);
        if (code) goto _err;
H
Hongze Cheng 已提交
94 95 96 97
      }
    }
  }

H
Hongze Cheng 已提交
98
  // TSDB ==============
H
Hongze Cheng 已提交
99
  if (!pReader->tsdbDone) {
H
Hongze Cheng 已提交
100
    // open if not
H
Hongze Cheng 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    if (pReader->pTsdbReader == NULL) {
      code = tsdbSnapReaderOpen(pReader->pVnode->pTsdb, pReader->sver, pReader->ever, &pReader->pTsdbReader);
      if (code) goto _err;
    }

    code = tsdbSnapRead(pReader->pTsdbReader, ppData);
    if (code) {
      goto _err;
    } else {
      if (*ppData) {
        goto _exit;
      } else {
        pReader->tsdbDone = 1;
        code = tsdbSnapReaderClose(&pReader->pTsdbReader);
        if (code) goto _err;
      }
    }
H
Hongze Cheng 已提交
118 119
  }

C
Cary Xu 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  // RSMA ==============
#if 0
  if (VND_IS_RSMA(pReader->pVnode)) {
    // RSMA1/RSMA2
    for (int32_t i = 0; i < TSDB_RETENTION_L2; ++i) {
      if (!pReader->rsmaDone[i]) {
        if (!pReader->pVnode->pSma->pRSmaTsdb[i]) {
          // no valid tsdb 
          pReader->rsmaDone[i] = 1;
          continue;
        }
        if (pReader->pTsdbReader == NULL) {
          code = tsdbSnapReaderOpen(pReader->pVnode->pSma->pRSmaTsdb[i], pReader->sver, pReader->ever,
                                    &pReader->pTsdbReader);
          if (code) goto _err;
        }

        code = tsdbSnapRead(pReader->pTsdbReader, ppData);
        if (code) {
          goto _err;
        } else {
          if (*ppData) {
            goto _exit;
          } else {
            pReader->tsdbDone = 1;
            code = tsdbSnapReaderClose(&pReader->pTsdbReader);
            if (code) goto _err;
          }
        }
      }
    }
    // QTaskInfoFile
    // TODO ...
  }
#endif

H
Hongze Cheng 已提交
156 157
  *ppData = NULL;
  *nData = 0;
H
Hongze Cheng 已提交
158 159

_exit:
H
Hongze Cheng 已提交
160
  if (*ppData) {
H
Hongze Cheng 已提交
161 162
    SSnapDataHdr *pHdr = (SSnapDataHdr *)(*ppData);

H
Hongze Cheng 已提交
163
    pReader->index++;
H
Hongze Cheng 已提交
164 165
    *nData = sizeof(SSnapDataHdr) + pHdr->size;
    pHdr->index = pReader->index;
H
Hongze Cheng 已提交
166 167
    vInfo("vgId:%d vnode snapshot read data,index:%" PRId64 " type:%d nData:%d ", TD_VID(pReader->pVnode),
          pReader->index, pHdr->type, *nData);
H
Hongze Cheng 已提交
168 169 170
  } else {
    vInfo("vgId:%d vnode snapshot read data end, index:%" PRId64, TD_VID(pReader->pVnode), pReader->index);
  }
H
Hongze Cheng 已提交
171 172 173
  return code;

_err:
H
Hongze Cheng 已提交
174
  vError("vgId:% vnode snapshot read failed since %s", TD_VID(pReader->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
175
  return code;
H
more  
Hongze Cheng 已提交
176 177
}

H
Hongze Cheng 已提交
178
// SVSnapWriter ========================================================
H
Hongze Cheng 已提交
179 180 181 182
struct SVSnapWriter {
  SVnode *pVnode;
  int64_t sver;
  int64_t ever;
H
Hongze Cheng 已提交
183
  int64_t index;
H
Hongze Cheng 已提交
184 185 186 187
  // meta
  SMetaSnapWriter *pMetaSnapWriter;
  // tsdb
  STsdbSnapWriter *pTsdbSnapWriter;
H
Hongze Cheng 已提交
188 189
};

H
Hongze Cheng 已提交
190
int32_t vnodeSnapWriterOpen(SVnode *pVnode, int64_t sver, int64_t ever, SVSnapWriter **ppWriter) {
H
Hongze Cheng 已提交
191 192
  int32_t       code = 0;
  SVSnapWriter *pWriter = NULL;
H
more  
Hongze Cheng 已提交
193 194

  // alloc
H
Hongze Cheng 已提交
195
  pWriter = (SVSnapWriter *)taosMemoryCalloc(1, sizeof(*pWriter));
H
more  
Hongze Cheng 已提交
196 197 198 199 200 201 202 203
  if (pWriter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pWriter->pVnode = pVnode;
  pWriter->sver = sver;
  pWriter->ever = ever;

H
Hongze Cheng 已提交
204 205
  vInfo("vgId:%d vnode snapshot writer opened", TD_VID(pVnode));
  *ppWriter = pWriter;
H
more  
Hongze Cheng 已提交
206 207 208
  return code;

_err:
H
Hongze Cheng 已提交
209
  vError("vgId:%d vnode snapshot writer open failed since %s", TD_VID(pVnode), tstrerror(code));
H
Hongze Cheng 已提交
210
  *ppWriter = NULL;
H
more  
Hongze Cheng 已提交
211 212 213
  return code;
}

214
int32_t vnodeSnapWriterClose(SVSnapWriter *pWriter, int8_t rollback, SSnapshot *pSnapshot) {
H
more  
Hongze Cheng 已提交
215
  int32_t code = 0;
H
Hongze Cheng 已提交
216
  SVnode *pVnode = pWriter->pVnode;
H
more  
Hongze Cheng 已提交
217

H
Hongze Cheng 已提交
218 219 220 221
  if (pWriter->pMetaSnapWriter) {
    code = metaSnapWriterClose(&pWriter->pMetaSnapWriter, rollback);
    if (code) goto _err;
  }
H
Hongze Cheng 已提交
222

H
Hongze Cheng 已提交
223 224 225 226
  if (pWriter->pTsdbSnapWriter) {
    code = tsdbSnapWriterClose(&pWriter->pTsdbSnapWriter, rollback);
    if (code) goto _err;
  }
H
more  
Hongze Cheng 已提交
227

H
Hongze Cheng 已提交
228 229 230 231 232 233
  if (!rollback) {
    SVnodeInfo info = {0};
    char       dir[TSDB_FILENAME_LEN];

    pVnode->state.committed = pWriter->ever;
    pVnode->state.applied = pWriter->ever;
234 235
    pVnode->state.applyTerm = pSnapshot->lastApplyTerm;
    pVnode->state.commitTerm = pSnapshot->lastApplyTerm;
H
Hongze Cheng 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

    info.config = pVnode->config;
    info.state.committed = pVnode->state.applied;
    info.state.commitTerm = pVnode->state.applyTerm;
    info.state.commitID = pVnode->state.commitID;
    snprintf(dir, TSDB_FILENAME_LEN, "%s%s%s", tfsGetPrimaryPath(pVnode->pTfs), TD_DIRSEP, pVnode->path);
    code = vnodeSaveInfo(dir, &info);
    if (code) goto _err;

    code = vnodeCommitInfo(dir, &info);
    if (code) goto _err;
  } else {
    ASSERT(0);
  }

H
Hongze Cheng 已提交
251
_exit:
H
Hongze Cheng 已提交
252
  vInfo("vgId:%d vnode snapshot writer closed, rollback:%d", TD_VID(pVnode), rollback);
H
more  
Hongze Cheng 已提交
253 254
  taosMemoryFree(pWriter);
  return code;
H
Hongze Cheng 已提交
255 256

_err:
H
Hongze Cheng 已提交
257
  vError("vgId:%d vnode snapshot writer close failed since %s", TD_VID(pWriter->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
258
  return code;
H
Hongze Cheng 已提交
259 260
}

H
Hongze Cheng 已提交
261
int32_t vnodeSnapWrite(SVSnapWriter *pWriter, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
262
  int32_t       code = 0;
H
Hongze Cheng 已提交
263
  SSnapDataHdr *pHdr = (SSnapDataHdr *)pData;
H
Hongze Cheng 已提交
264 265
  SVnode       *pVnode = pWriter->pVnode;

H
Hongze Cheng 已提交
266 267 268
  ASSERT(pHdr->size + sizeof(SSnapDataHdr) == nData);
  ASSERT(pHdr->index == pWriter->index + 1);
  pWriter->index = pHdr->index;
H
Hongze Cheng 已提交
269

H
Hongze Cheng 已提交
270 271
  vInfo("vgId:%d vnode snapshot write data, index:%" PRId64 " type:%d nData:%d", TD_VID(pVnode), pHdr->index,
        pHdr->type, nData);
H
Hongze Cheng 已提交
272

H
Hongze Cheng 已提交
273 274
  if (pHdr->type == 0) {
    // meta
H
Hongze Cheng 已提交
275

H
Hongze Cheng 已提交
276 277 278 279
    if (pWriter->pMetaSnapWriter == NULL) {
      code = metaSnapWriterOpen(pVnode->pMeta, pWriter->sver, pWriter->ever, &pWriter->pMetaSnapWriter);
      if (code) goto _err;
    }
H
Hongze Cheng 已提交
280

H
Hongze Cheng 已提交
281 282
    code = metaSnapWrite(pWriter->pMetaSnapWriter, pData, nData);
    if (code) goto _err;
H
Hongze Cheng 已提交
283 284 285 286 287 288 289 290 291 292 293
  } else {
    // tsdb

    if (pWriter->pTsdbSnapWriter == NULL) {
      code = tsdbSnapWriterOpen(pVnode->pTsdb, pWriter->sver, pWriter->ever, &pWriter->pTsdbSnapWriter);
      if (code) goto _err;
    }

    code = tsdbSnapWrite(pWriter->pTsdbSnapWriter, pData, nData);
    if (code) goto _err;
  }
H
Hongze Cheng 已提交
294

H
Hongze Cheng 已提交
295
_exit:
H
Hongze Cheng 已提交
296 297 298
  return code;

_err:
H
Hongze Cheng 已提交
299 300
  vError("vgId:%d vnode snapshot write failed since %s, index:%" PRId64 " type:%d nData:%d", TD_VID(pVnode),
         tstrerror(code), pHdr->index, pHdr->type, nData);
H
Hongze Cheng 已提交
301
  return code;
H
Hongze Cheng 已提交
302
}