vnodeSnapshot.c 7.2 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;
H
Hongze Cheng 已提交
30 31
};

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
74
  // META ==============
H
Hongze Cheng 已提交
75
  if (!pReader->metaDone) {
H
Hongze Cheng 已提交
76 77 78 79 80 81 82
    // 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 已提交
83
    if (code) {
H
Hongze Cheng 已提交
84 85 86 87
      goto _err;
    } else {
      if (*ppData) {
        goto _exit;
H
Hongze Cheng 已提交
88
      } else {
H
Hongze Cheng 已提交
89 90 91
        pReader->metaDone = 1;
        code = metaSnapReaderClose(&pReader->pMetaReader);
        if (code) goto _err;
H
Hongze Cheng 已提交
92 93 94 95
      }
    }
  }

H
Hongze Cheng 已提交
96
  // TSDB ==============
H
Hongze Cheng 已提交
97
  if (!pReader->tsdbDone) {
H
Hongze Cheng 已提交
98
    // open if not
H
Hongze Cheng 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    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 已提交
116 117
  }

H
Hongze Cheng 已提交
118 119
  *ppData = NULL;
  *nData = 0;
H
Hongze Cheng 已提交
120 121

_exit:
H
Hongze Cheng 已提交
122
  if (*ppData) {
H
Hongze Cheng 已提交
123 124
    SSnapDataHdr *pHdr = (SSnapDataHdr *)(*ppData);

H
Hongze Cheng 已提交
125
    pReader->index++;
H
Hongze Cheng 已提交
126 127
    *nData = sizeof(SSnapDataHdr) + pHdr->size;
    pHdr->index = pReader->index;
H
Hongze Cheng 已提交
128 129
    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 已提交
130 131 132
  } else {
    vInfo("vgId:%d vnode snapshot read data end, index:%" PRId64, TD_VID(pReader->pVnode), pReader->index);
  }
H
Hongze Cheng 已提交
133 134 135
  return code;

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

H
Hongze Cheng 已提交
140
// SVSnapWriter ========================================================
H
Hongze Cheng 已提交
141 142 143 144
struct SVSnapWriter {
  SVnode *pVnode;
  int64_t sver;
  int64_t ever;
H
Hongze Cheng 已提交
145
  int64_t index;
H
Hongze Cheng 已提交
146 147 148 149
  // meta
  SMetaSnapWriter *pMetaSnapWriter;
  // tsdb
  STsdbSnapWriter *pTsdbSnapWriter;
H
Hongze Cheng 已提交
150 151
};

H
Hongze Cheng 已提交
152
int32_t vnodeSnapWriterOpen(SVnode *pVnode, int64_t sver, int64_t ever, SVSnapWriter **ppWriter) {
H
Hongze Cheng 已提交
153 154
  int32_t       code = 0;
  SVSnapWriter *pWriter = NULL;
H
more  
Hongze Cheng 已提交
155 156

  // alloc
H
Hongze Cheng 已提交
157
  pWriter = (SVSnapWriter *)taosMemoryCalloc(1, sizeof(*pWriter));
H
more  
Hongze Cheng 已提交
158 159 160 161 162 163 164 165
  if (pWriter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pWriter->pVnode = pVnode;
  pWriter->sver = sver;
  pWriter->ever = ever;

H
Hongze Cheng 已提交
166 167
  vInfo("vgId:%d vnode snapshot writer opened", TD_VID(pVnode));
  *ppWriter = pWriter;
H
more  
Hongze Cheng 已提交
168 169 170
  return code;

_err:
H
Hongze Cheng 已提交
171
  vError("vgId:%d vnode snapshot writer open failed since %s", TD_VID(pVnode), tstrerror(code));
H
Hongze Cheng 已提交
172
  *ppWriter = NULL;
H
more  
Hongze Cheng 已提交
173 174 175
  return code;
}

176
int32_t vnodeSnapWriterClose(SVSnapWriter *pWriter, int8_t rollback, SSnapshot *pSnapshot) {
H
more  
Hongze Cheng 已提交
177
  int32_t code = 0;
H
Hongze Cheng 已提交
178
  SVnode *pVnode = pWriter->pVnode;
H
more  
Hongze Cheng 已提交
179

H
Hongze Cheng 已提交
180 181 182 183
  if (pWriter->pMetaSnapWriter) {
    code = metaSnapWriterClose(&pWriter->pMetaSnapWriter, rollback);
    if (code) goto _err;
  }
H
Hongze Cheng 已提交
184

H
Hongze Cheng 已提交
185 186 187 188
  if (pWriter->pTsdbSnapWriter) {
    code = tsdbSnapWriterClose(&pWriter->pTsdbSnapWriter, rollback);
    if (code) goto _err;
  }
H
more  
Hongze Cheng 已提交
189

H
Hongze Cheng 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
  if (!rollback) {
    SVnodeInfo info = {0};
    char       dir[TSDB_FILENAME_LEN];

    pVnode->state.committed = pWriter->ever;
    pVnode->state.applied = pWriter->ever;
    // pVnode->state.applyTerm = ;
    // pVnode->state.commitTerm = ;

    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 已提交
213
_exit:
H
Hongze Cheng 已提交
214
  vInfo("vgId:%d vnode snapshot writer closed, rollback:%d", TD_VID(pVnode), rollback);
H
more  
Hongze Cheng 已提交
215 216
  taosMemoryFree(pWriter);
  return code;
H
Hongze Cheng 已提交
217 218

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

H
Hongze Cheng 已提交
223
int32_t vnodeSnapWrite(SVSnapWriter *pWriter, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
224
  int32_t       code = 0;
H
Hongze Cheng 已提交
225
  SSnapDataHdr *pHdr = (SSnapDataHdr *)pData;
H
Hongze Cheng 已提交
226 227
  SVnode       *pVnode = pWriter->pVnode;

H
Hongze Cheng 已提交
228 229 230
  ASSERT(pHdr->size + sizeof(SSnapDataHdr) == nData);
  ASSERT(pHdr->index == pWriter->index + 1);
  pWriter->index = pHdr->index;
H
Hongze Cheng 已提交
231

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

H
Hongze Cheng 已提交
235 236
  if (pHdr->type == 0) {
    // meta
H
Hongze Cheng 已提交
237

H
Hongze Cheng 已提交
238 239 240 241
    if (pWriter->pMetaSnapWriter == NULL) {
      code = metaSnapWriterOpen(pVnode->pMeta, pWriter->sver, pWriter->ever, &pWriter->pMetaSnapWriter);
      if (code) goto _err;
    }
H
Hongze Cheng 已提交
242

H
Hongze Cheng 已提交
243 244
    code = metaSnapWrite(pWriter->pMetaSnapWriter, pData, nData);
    if (code) goto _err;
H
Hongze Cheng 已提交
245 246 247 248 249 250 251 252 253 254 255
  } 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 已提交
256

H
Hongze Cheng 已提交
257
_exit:
H
Hongze Cheng 已提交
258 259 260
  return code;

_err:
H
Hongze Cheng 已提交
261 262
  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 已提交
263
  return code;
H
Hongze Cheng 已提交
264
}