metaSnapshot.c 4.3 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 "meta.h"

H
Hongze Cheng 已提交
18
// SMetaSnapReader ========================================
H
Hongze Cheng 已提交
19
struct SMetaSnapReader {
H
Hongze Cheng 已提交
20 21 22
  SMeta*  pMeta;
  int64_t sver;
  int64_t ever;
H
Hongze Cheng 已提交
23
  TBC*    pTbc;
H
Hongze Cheng 已提交
24 25
};

H
Hongze Cheng 已提交
26
int32_t metaSnapReaderOpen(SMeta* pMeta, int64_t sver, int64_t ever, SMetaSnapReader** ppReader) {
H
Hongze Cheng 已提交
27 28
  int32_t          code = 0;
  int32_t          c = 0;
H
Hongze Cheng 已提交
29
  SMetaSnapReader* pMetaSnapReader = NULL;
H
Hongze Cheng 已提交
30

H
Hongze Cheng 已提交
31 32 33
  // alloc
  pMetaSnapReader = (SMetaSnapReader*)taosMemoryCalloc(1, sizeof(*pMetaSnapReader));
  if (pMetaSnapReader == NULL) {
H
Hongze Cheng 已提交
34 35 36
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
37 38 39 40 41 42
  pMetaSnapReader->pMeta = pMeta;
  pMetaSnapReader->sver = sver;
  pMetaSnapReader->ever = ever;

  // impl
  code = tdbTbcOpen(pMeta->pTbDb, &pMetaSnapReader->pTbc, NULL);
H
Hongze Cheng 已提交
43 44 45 46
  if (code) {
    goto _err;
  }

H
Hongze Cheng 已提交
47
  code = tdbTbcMoveTo(pMetaSnapReader->pTbc, &(STbDbKey){.version = sver, .uid = INT64_MIN}, sizeof(STbDbKey), &c);
H
Hongze Cheng 已提交
48 49 50 51
  if (code) {
    goto _err;
  }

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

_err:
H
Hongze Cheng 已提交
56
  metaError("vgId:%d meta snap reader open failed since %s", TD_VID(pMeta->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
57 58
  *ppReader = NULL;
  return code;
H
Hongze Cheng 已提交
59 60
}

H
Hongze Cheng 已提交
61 62 63 64
int32_t metaSnapReaderClose(SMetaSnapReader** ppReader) {
  tdbTbcClose((*ppReader)->pTbc);
  taosMemoryFree(*ppReader);
  *ppReader = NULL;
H
Hongze Cheng 已提交
65 66 67
  return 0;
}

H
Hongze Cheng 已提交
68
int32_t metaSnapRead(SMetaSnapReader* pReader, uint8_t** ppData) {
H
Hongze Cheng 已提交
69 70 71 72 73 74 75 76 77
  const void* pKey = NULL;
  const void* pData = NULL;
  int32_t     nKey = 0;
  int32_t     nData = 0;
  int32_t     code = 0;

  for (;;) {
    code = tdbTbcGet(pReader->pTbc, &pKey, &nKey, &pData, &nData);
    if (code || ((STbDbKey*)pData)->version > pReader->ever) {
H
Hongze Cheng 已提交
78 79
      code = TSDB_CODE_VND_READ_END;
      goto _exit;
H
Hongze Cheng 已提交
80 81 82
    }

    if (((STbDbKey*)pData)->version < pReader->sver) {
H
Hongze Cheng 已提交
83
      tdbTbcMoveToNext(pReader->pTbc);
H
Hongze Cheng 已提交
84 85 86
      continue;
    }

H
Hongze Cheng 已提交
87
    tdbTbcMoveToNext(pReader->pTbc);
H
Hongze Cheng 已提交
88 89 90 91
    break;
  }

  // copy the data
H
Hongze Cheng 已提交
92
  if (tRealloc(ppData, sizeof(SSnapDataHdr) + nData) < 0) {
H
Hongze Cheng 已提交
93 94 95
    code = TSDB_CODE_OUT_OF_MEMORY;
    return code;
  }
H
Hongze Cheng 已提交
96 97 98
  ((SSnapDataHdr*)(*ppData))->type = 0;  // TODO: use macro
  ((SSnapDataHdr*)(*ppData))->size = nData;
  memcpy(((SSnapDataHdr*)(*ppData))->data, pData, nData);
H
Hongze Cheng 已提交
99 100

_exit:
H
Hongze Cheng 已提交
101
  return code;
H
Hongze Cheng 已提交
102 103
}

H
Hongze Cheng 已提交
104 105 106 107 108 109 110
// SMetaSnapWriter ========================================
struct SMetaSnapWriter {
  SMeta*  pMeta;
  int64_t sver;
  int64_t ever;
};

H
Hongze Cheng 已提交
111
static int32_t metaSnapRollback(SMetaSnapWriter* pWriter) {
H
Hongze Cheng 已提交
112 113 114 115 116
  int32_t code = 0;
  // TODO
  return code;
}

H
Hongze Cheng 已提交
117
static int32_t metaSnapCommit(SMetaSnapWriter* pWriter) {
H
Hongze Cheng 已提交
118 119 120 121 122
  int32_t code = 0;
  // TODO
  return code;
}

H
Hongze Cheng 已提交
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 156 157 158 159 160 161 162 163 164 165 166
int32_t metaSnapWriterOpen(SMeta* pMeta, int64_t sver, int64_t ever, SMetaSnapWriter** ppWriter) {
  int32_t          code = 0;
  SMetaSnapWriter* pWriter;

  // alloc
  pWriter = (SMetaSnapWriter*)taosMemoryCalloc(1, sizeof(*pWriter));
  if (pWriter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pWriter->pMeta = pMeta;
  pWriter->sver = sver;
  pWriter->ever = ever;

  *ppWriter = pWriter;
  return code;

_err:
  metaError("vgId:%d meta snapshot writer open failed since %s", TD_VID(pMeta->pVnode), tstrerror(code));
  *ppWriter = NULL;
  return code;
}

int32_t metaSnapWriterClose(SMetaSnapWriter** ppWriter, int8_t rollback) {
  int32_t          code = 0;
  SMetaSnapWriter* pWriter = *ppWriter;

  if (rollback) {
    code = metaSnapRollback(pWriter);
    if (code) goto _err;
  } else {
    code = metaSnapCommit(pWriter);
    if (code) goto _err;
  }
  taosMemoryFree(pWriter);
  *ppWriter = NULL;

  return code;

_err:
  metaError("vgId:%d meta snapshot writer close failed since %s", TD_VID(pWriter->pMeta->pVnode), tstrerror(code));
  return code;
}

H
Hongze Cheng 已提交
167
int32_t metaSnapWrite(SMetaSnapWriter* pWriter, uint8_t* pData, uint32_t nData) {
H
Hongze Cheng 已提交
168 169 170 171
  int32_t code = 0;
  // TODO
  return code;
}