tqOffsetSnapshot.c 4.1 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*
 * 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"
#include "tdbInt.h"
#include "tq.h"

// STqOffsetReader ========================================
struct STqOffsetReader {
  STQ*    pTq;
  int64_t sver;
  int64_t ever;
  int8_t  readEnd;
};

int32_t tqOffsetReaderOpen(STQ* pTq, int64_t sver, int64_t ever, STqOffsetReader** ppReader) {
  STqOffsetReader* pReader = NULL;

  pReader = taosMemoryCalloc(1, sizeof(STqOffsetReader));
  if (pReader == NULL) {
    *ppReader = NULL;
    return -1;
  }
  pReader->pTq = pTq;
  pReader->sver = sver;
  pReader->ever = ever;

C
Cary Xu 已提交
40
  tqInfo("vgId:%d, vnode snapshot tq offset reader opened", TD_VID(pTq->pVnode));
L
Liu Jicong 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

  *ppReader = pReader;
  return 0;
}

int32_t tqOffsetReaderClose(STqOffsetReader** ppReader) {
  taosMemoryFree(*ppReader);
  *ppReader = NULL;
  return 0;
}

int32_t tqOffsetSnapRead(STqOffsetReader* pReader, uint8_t** ppData) {
  if (pReader->readEnd != 0) return 0;

  char*     fname = tqOffsetBuildFName(pReader->pTq->path, 0);
  TdFilePtr pFile = taosOpenFile(fname, TD_FILE_READ);
57
  if (pFile == NULL) {
L
Liu Jicong 已提交
58
    taosMemoryFree(fname);
L
Liu Jicong 已提交
59 60 61 62 63
    return 0;
  }

  int64_t sz = 0;
  if (taosStatFile(fname, &sz, NULL) < 0) {
64
    tAssert(0);
L
Liu Jicong 已提交
65
  }
L
Liu Jicong 已提交
66
  taosMemoryFree(fname);
L
Liu Jicong 已提交
67 68 69 70 71 72 73 74 75

  SSnapDataHdr* buf = taosMemoryCalloc(1, sz + sizeof(SSnapDataHdr));
  if (buf == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return terrno;
  }
  void*   abuf = POINTER_SHIFT(buf, sizeof(SSnapDataHdr));
  int64_t contLen = taosReadFile(pFile, abuf, sz);
  if (contLen != sz) {
76
    tAssert(0);
L
Liu Jicong 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    return -1;
  }
  buf->size = sz;
  buf->type = SNAP_DATA_TQ_OFFSET;
  *ppData = (uint8_t*)buf;

  pReader->readEnd = 1;
  return 0;
}

// STqOffseWriter ========================================
struct STqOffsetWriter {
  STQ*    pTq;
  int64_t sver;
  int64_t ever;
  int32_t tmpFileVer;
  char*   fname;
};

int32_t tqOffsetWriterOpen(STQ* pTq, int64_t sver, int64_t ever, STqOffsetWriter** ppWriter) {
  int32_t          code = 0;
  STqOffsetWriter* pWriter;

  pWriter = (STqOffsetWriter*)taosMemoryCalloc(1, sizeof(STqOffsetWriter));
  if (pWriter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pWriter->pTq = pTq;
  pWriter->sver = sver;
  pWriter->ever = ever;

  *ppWriter = pWriter;
  return code;

_err:
C
Cary Xu 已提交
113
  tqError("vgId:%d, tq snapshot writer open failed since %s", TD_VID(pTq->pVnode), tstrerror(code));
L
Liu Jicong 已提交
114 115 116 117 118 119 120 121 122 123
  *ppWriter = NULL;
  return code;
}

int32_t tqOffsetWriterClose(STqOffsetWriter** ppWriter, int8_t rollback) {
  STqOffsetWriter* pWriter = *ppWriter;
  STQ*             pTq = pWriter->pTq;
  char*            fname = tqOffsetBuildFName(pTq->path, 0);

  if (rollback) {
L
Liu Jicong 已提交
124
    if (taosRemoveFile(pWriter->fname) < 0) {
125
      tAssert(0);
L
Liu Jicong 已提交
126
    }
L
Liu Jicong 已提交
127
  } else {
L
Liu Jicong 已提交
128
    if (taosRenameFile(pWriter->fname, fname) < 0) {
129
      tAssert(0);
L
Liu Jicong 已提交
130
    }
L
Liu Jicong 已提交
131
    if (tqOffsetRestoreFromFile(pTq->pOffsetStore, fname) < 0) {
132
      tAssert(0);
L
Liu Jicong 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    }
  }
  taosMemoryFree(fname);
  taosMemoryFree(pWriter->fname);
  taosMemoryFree(pWriter);
  *ppWriter = NULL;
  return 0;
}

int32_t tqOffsetSnapWrite(STqOffsetWriter* pWriter, uint8_t* pData, uint32_t nData) {
  STQ* pTq = pWriter->pTq;
  pWriter->tmpFileVer = 1;
  pWriter->fname = tqOffsetBuildFName(pTq->path, pWriter->tmpFileVer);
  TdFilePtr     pFile = taosOpenFile(pWriter->fname, TD_FILE_CREATE | TD_FILE_WRITE);
  SSnapDataHdr* pHdr = (SSnapDataHdr*)pData;
  int64_t       size = pHdr->size;
  ASSERT(size == nData - sizeof(SSnapDataHdr));
  if (pFile) {
    int64_t contLen = taosWriteFile(pFile, pHdr->data, size);
    if (contLen != size) {
153
      tAssert(0);
L
Liu Jicong 已提交
154 155
    }
  } else {
156
    tAssert(0);
L
Liu Jicong 已提交
157 158 159 160
    return -1;
  }
  return 0;
}