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

#include "vnodeInt.h"

H
Hongze Cheng 已提交
18
struct SVSnapReader {
H
Hongze Cheng 已提交
19 20 21 22 23
  SVnode *pVnode;
  int64_t sver;
  int64_t ever;
  // meta
  int8_t           metaDone;
H
Hongze Cheng 已提交
24
  SMetaSnapReader *pMetaReader;
H
Hongze Cheng 已提交
25 26
  // tsdb
  int8_t           tsdbDone;
H
Hongze Cheng 已提交
27 28 29
  STsdbSnapReader *pTsdbReader;
  void            *pData;
  int32_t          nData;
H
Hongze Cheng 已提交
30 31
};

H
Hongze Cheng 已提交
32
struct SVSnapWriter {
H
more  
Hongze Cheng 已提交
33 34 35 36 37
  SVnode *pVnode;
  int64_t sver;
  int64_t ever;
};

H
Hongze Cheng 已提交
38
// SVSnapReader ========================================================
H
Hongze Cheng 已提交
39
int32_t vnodeSnapReaderOpen(SVnode *pVnode, int64_t sver, int64_t ever, SVSnapReader **ppReader) {
H
Hongze Cheng 已提交
40
  int32_t       code = 0;
H
Hongze Cheng 已提交
41
  SVSnapReader *pReader = NULL;
H
Hongze Cheng 已提交
42

H
Hongze Cheng 已提交
43
  pReader = (SVSnapReader *)taosMemoryCalloc(1, sizeof(*pReader));
H
Hongze Cheng 已提交
44 45 46 47 48 49 50 51
  if (pReader == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pReader->pVnode = pVnode;
  pReader->sver = sver;
  pReader->ever = ever;

H
Hongze Cheng 已提交
52
  if (metaSnapReaderOpen(pVnode->pMeta, &pReader->pMetaReader, sver, ever) < 0) {
H
Hongze Cheng 已提交
53 54 55 56
    taosMemoryFree(pReader);
    goto _err;
  }

H
Hongze Cheng 已提交
57 58
  if (tsdbSnapReaderOpen(pVnode->pTsdb, &pReader->pTsdbReader, sver, ever) < 0) {
    metaSnapReaderClose(pReader->pMetaReader);
H
Hongze Cheng 已提交
59 60 61 62 63
    taosMemoryFree(pReader);
    goto _err;
  }

  *ppReader = pReader;
H
Hongze Cheng 已提交
64
  return code;
H
Hongze Cheng 已提交
65 66 67

_err:
  *ppReader = NULL;
H
Hongze Cheng 已提交
68
  return code;
H
Hongze Cheng 已提交
69 70
}

H
Hongze Cheng 已提交
71
int32_t vnodeSnapReaderClose(SVSnapReader *pReader) {
H
Hongze Cheng 已提交
72 73
  int32_t code = 0;

H
Hongze Cheng 已提交
74 75 76 77 78
  vnodeFree(pReader->pData);
  tsdbSnapReaderClose(pReader->pTsdbReader);
  metaSnapReaderClose(pReader->pMetaReader);
  taosMemoryFree(pReader);

H
Hongze Cheng 已提交
79
  return code;
H
Hongze Cheng 已提交
80 81
}

H
Hongze Cheng 已提交
82
int32_t vnodeSnapRead(SVSnapReader *pReader, const void **ppData, uint32_t *nData) {
H
Hongze Cheng 已提交
83 84
  int32_t code = 0;

H
Hongze Cheng 已提交
85
  if (!pReader->metaDone) {
H
Hongze Cheng 已提交
86
    code = metaSnapRead(pReader->pMetaReader, &pReader->pData, &pReader->nData);
H
Hongze Cheng 已提交
87 88
    if (code) {
      if (code == TSDB_CODE_VND_READ_END) {
H
Hongze Cheng 已提交
89
        pReader->metaDone = 1;
H
Hongze Cheng 已提交
90 91 92 93
      } else {
        return code;
      }
    } else {
H
Hongze Cheng 已提交
94 95
      *ppData = pReader->pData;
      *nData = pReader->nData;
H
Hongze Cheng 已提交
96 97 98 99
      return code;
    }
  }

H
Hongze Cheng 已提交
100
  if (!pReader->tsdbDone) {
H
Hongze Cheng 已提交
101
    code = tsdbSnapRead(pReader->pTsdbReader, &pReader->pData, &pReader->nData);
H
Hongze Cheng 已提交
102
    if (code) {
H
Hongze Cheng 已提交
103
      if (code == TSDB_CODE_VND_READ_END) {
H
Hongze Cheng 已提交
104
        pReader->tsdbDone = 1;
H
Hongze Cheng 已提交
105 106 107
      } else {
        return code;
      }
H
Hongze Cheng 已提交
108
    } else {
H
Hongze Cheng 已提交
109 110
      *ppData = pReader->pData;
      *nData = pReader->nData;
H
Hongze Cheng 已提交
111 112 113 114 115 116
      return code;
    }
  }

  code = TSDB_CODE_VND_READ_END;
  return code;
H
more  
Hongze Cheng 已提交
117 118
}

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

  // alloc
H
Hongze Cheng 已提交
125
  pWriter = (SVSnapWriter *)taosMemoryCalloc(1, sizeof(*pWriter));
H
more  
Hongze Cheng 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139
  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 已提交
140
int32_t vnodeSnapshotWriterClose(SVSnapWriter *pWriter, int8_t rollback) {
H
more  
Hongze Cheng 已提交
141 142 143 144 145 146 147 148 149 150
  int32_t code = 0;

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

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

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