vnodeSnapshot.c 3.9 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 23 24
  SVnode *pVnode;
  int64_t sver;
  int64_t ever;
  // meta
  int8_t           metaDone;
H
Hongze Cheng 已提交
25
  SMetaSnapReader *pMetaReader;
H
Hongze Cheng 已提交
26 27
  // tsdb
  int8_t           tsdbDone;
H
Hongze Cheng 已提交
28
  STsdbSnapReader *pTsdbReader;
H
Hongze Cheng 已提交
29
  uint8_t         *pData;
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 46
  code = metaSnapReaderOpen(pVnode->pMeta, sver, ever, &pReader->pMetaReader);
  if (code) goto _err;
H
Hongze Cheng 已提交
47

H
Hongze Cheng 已提交
48 49
  code = tsdbSnapReaderOpen(pVnode->pTsdb, sver, ever, &pReader->pTsdbReader);
  if (code) goto _err;
H
Hongze Cheng 已提交
50 51

  *ppReader = pReader;
H
Hongze Cheng 已提交
52
  return code;
H
Hongze Cheng 已提交
53 54

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

H
Hongze Cheng 已提交
60
int32_t vnodeSnapReaderClose(SVSnapReader *pReader) {
H
Hongze Cheng 已提交
61 62
  int32_t code = 0;

H
Hongze Cheng 已提交
63 64 65
  tFree(pReader->pData);
  if (pReader->pTsdbReader) tsdbSnapReaderClose(&pReader->pTsdbReader);
  if (pReader->pMetaReader) metaSnapReaderClose(&pReader->pMetaReader);
H
Hongze Cheng 已提交
66 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
  if (!pReader->metaDone) {
H
Hongze Cheng 已提交
75
    code = metaSnapRead(pReader->pMetaReader, &pReader->pData);
H
Hongze Cheng 已提交
76 77
    if (code) {
      if (code == TSDB_CODE_VND_READ_END) {
H
Hongze Cheng 已提交
78
        pReader->metaDone = 1;
H
Hongze Cheng 已提交
79
      } else {
H
Hongze Cheng 已提交
80
        goto _err;
H
Hongze Cheng 已提交
81 82
      }
    } else {
H
Hongze Cheng 已提交
83
      *ppData = pReader->pData;
H
Hongze Cheng 已提交
84
      *nData = sizeof(SSnapDataHdr) + ((SSnapDataHdr *)pReader->pData)->size;
H
Hongze Cheng 已提交
85
      goto _exit;
H
Hongze Cheng 已提交
86 87 88
    }
  }

H
Hongze Cheng 已提交
89
  if (!pReader->tsdbDone) {
H
Hongze Cheng 已提交
90
    code = tsdbSnapRead(pReader->pTsdbReader, &pReader->pData, &pReader->nData);
H
Hongze Cheng 已提交
91
    if (code) {
H
Hongze Cheng 已提交
92
      if (code == TSDB_CODE_VND_READ_END) {
H
Hongze Cheng 已提交
93
        pReader->tsdbDone = 1;
H
Hongze Cheng 已提交
94
      } else {
H
Hongze Cheng 已提交
95
        goto _err;
H
Hongze Cheng 已提交
96
      }
H
Hongze Cheng 已提交
97
    } else {
H
Hongze Cheng 已提交
98
      *ppData = pReader->pData;
H
Hongze Cheng 已提交
99
      *nData = sizeof(SSnapDataHdr) + ((SSnapDataHdr *)pReader->pData)->size;
H
Hongze Cheng 已提交
100
      goto _exit;
H
Hongze Cheng 已提交
101 102 103 104
    }
  }

  code = TSDB_CODE_VND_READ_END;
H
Hongze Cheng 已提交
105 106 107 108 109 110

_exit:
  return code;

_err:
  vError("vgId:% snapshot read failed since %s", TD_VID(pReader->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
111
  return code;
H
more  
Hongze Cheng 已提交
112 113
}

H
Hongze Cheng 已提交
114
// SVSnapWriter ========================================================
H
Hongze Cheng 已提交
115 116 117 118 119 120
struct SVSnapWriter {
  SVnode *pVnode;
  int64_t sver;
  int64_t ever;
};

H
Hongze Cheng 已提交
121 122 123
int32_t vnodeSnapshotWriterOpen(SVnode *pVnode, int64_t sver, int64_t ever, SVSnapWriter **ppWriter) {
  int32_t       code = 0;
  SVSnapWriter *pWriter = NULL;
H
more  
Hongze Cheng 已提交
124 125

  // alloc
H
Hongze Cheng 已提交
126
  pWriter = (SVSnapWriter *)taosMemoryCalloc(1, sizeof(*pWriter));
H
more  
Hongze Cheng 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140
  if (pWriter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pWriter->pVnode = pVnode;
  pWriter->sver = sver;
  pWriter->ever = ever;

  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
141
int32_t vnodeSnapshotWriterClose(SVSnapWriter *pWriter, int8_t rollback) {
H
more  
Hongze Cheng 已提交
142 143 144 145 146 147 148 149 150 151
  int32_t code = 0;

  if (!rollback) {
    // apply the change
  } else {
    // rollback the change
  }

  taosMemoryFree(pWriter);
  return code;
H
Hongze Cheng 已提交
152 153 154 155 156 157
}

int32_t vnodeSnapshotWrite(SVSnapWriter *pWriter, uint8_t *pData, uint32_t nData) {
  int32_t code = 0;
  // TODO
  return code;
H
Hongze Cheng 已提交
158
}