tqOffsetSnapshot.c 4.3 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);
59
    return 0;
L
Liu Jicong 已提交
60 61 62 63
  }

  int64_t sz = 0;
  if (taosStatFile(fname, &sz, NULL) < 0) {
L
Liu Jicong 已提交
64 65 66
    taosCloseFile(&pFile);
    taosMemoryFree(fname);
    return -1;
L
Liu Jicong 已提交
67
  }
L
Liu Jicong 已提交
68
  taosMemoryFree(fname);
L
Liu Jicong 已提交
69 70 71 72

  SSnapDataHdr* buf = taosMemoryCalloc(1, sz + sizeof(SSnapDataHdr));
  if (buf == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
73
    taosCloseFile(&pFile);
L
Liu Jicong 已提交
74 75 76 77 78
    return terrno;
  }
  void*   abuf = POINTER_SHIFT(buf, sizeof(SSnapDataHdr));
  int64_t contLen = taosReadFile(pFile, abuf, sz);
  if (contLen != sz) {
L
Liu Jicong 已提交
79 80
    taosCloseFile(&pFile);
    taosMemoryFree(buf);
L
Liu Jicong 已提交
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 113 114 115 116
    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 已提交
117
  tqError("vgId:%d, tq snapshot writer open failed since %s", TD_VID(pTq->pVnode), tstrerror(code));
L
Liu Jicong 已提交
118 119 120 121 122 123 124 125 126 127
  *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 已提交
128
    if (taosRemoveFile(pWriter->fname) < 0) {
L
Liu Jicong 已提交
129 130
      taosMemoryFree(fname);
      return -1;
L
Liu Jicong 已提交
131
    }
L
Liu Jicong 已提交
132
  } else {
L
Liu Jicong 已提交
133
    if (taosRenameFile(pWriter->fname, fname) < 0) {
L
Liu Jicong 已提交
134 135
      taosMemoryFree(fname);
      return -1;
L
Liu Jicong 已提交
136
    }
L
Liu Jicong 已提交
137
    if (tqOffsetRestoreFromFile(pTq->pOffsetStore, fname) < 0) {
L
Liu Jicong 已提交
138 139
      taosMemoryFree(fname);
      return -1;
L
Liu Jicong 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    }
  }
  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;
  if (pFile) {
    int64_t contLen = taosWriteFile(pFile, pHdr->data, size);
    if (contLen != size) {
L
Liu Jicong 已提交
159 160
      taosCloseFile(&pFile);
      return -1;
L
Liu Jicong 已提交
161 162 163 164 165 166
    }
  } else {
    return -1;
  }
  return 0;
}