walSeek.c 4.2 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
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
#include "os.h"
#include "taoserror.h"
#include "tfile.h"
L
Liu Jicong 已提交
20
#include "tref.h"
L
Liu Jicong 已提交
21 22
#include "walInt.h"

L
Liu Jicong 已提交
23
static int walSeekWritePos(SWal* pWal, int64_t ver) {
L
Liu Jicong 已提交
24
  int code = 0;
L
Liu Jicong 已提交
25

26 27
  TdFilePtr pIdxTFile = pWal->pWriteIdxTFile;
  TdFilePtr pLogTFile = pWal->pWriteLogTFile;
L
Liu Jicong 已提交
28 29

  // seek position
L
Liu Jicong 已提交
30
  int64_t idxOff = walGetVerIdxOffset(pWal, ver);
31
  code = taosLSeekFile(pIdxTFile, idxOff, SEEK_SET);
L
Liu Jicong 已提交
32
  if (code != 0) {
L
Liu Jicong 已提交
33
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
34
    return -1;
L
Liu Jicong 已提交
35
  }
L
Liu Jicong 已提交
36
  SWalIdxEntry entry;
L
Liu Jicong 已提交
37
  // TODO:deserialize
38
  code = taosReadFile(pIdxTFile, &entry, sizeof(SWalIdxEntry));
L
Liu Jicong 已提交
39
  if (code != 0) {
L
Liu Jicong 已提交
40
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
41
    return -1;
L
Liu Jicong 已提交
42
  }
L
Liu Jicong 已提交
43
  ASSERT(entry.ver == ver);
44
  code = taosLSeekFile(pLogTFile, entry.offset, SEEK_SET);
L
Liu Jicong 已提交
45
  if (code < 0) {
L
Liu Jicong 已提交
46
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
47
    return -1;
L
Liu Jicong 已提交
48 49 50 51
  }
  return code;
}

L
Liu Jicong 已提交
52
int walSetWrite(SWal* pWal) {
53
  TdFilePtr     pIdxTFile, pLogTFile;
L
Liu Jicong 已提交
54
  SWalFileInfo* pRet = taosArrayGetLast(pWal->fileInfoSet);
L
Liu Jicong 已提交
55 56 57 58 59
  ASSERT(pRet != NULL);
  int64_t fileFirstVer = pRet->firstVer;

  char fnameStr[WAL_FILE_LEN];
  walBuildIdxName(pWal, fileFirstVer, fnameStr);
60
  pIdxTFile = taosOpenFile(fnameStr, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_APPEND);
61
  if (pIdxTFile == NULL) {
L
Liu Jicong 已提交
62
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
63 64 65
    return -1;
  }
  walBuildLogName(pWal, fileFirstVer, fnameStr);
66
  pLogTFile = taosOpenFile(fnameStr, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_APPEND);
67
  if (pLogTFile == NULL) {
L
Liu Jicong 已提交
68
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
69 70
    return -1;
  }
L
Liu Jicong 已提交
71
  // switch file
72 73
  pWal->pWriteIdxTFile = pIdxTFile;
  pWal->pWriteLogTFile = pLogTFile;
L
Liu Jicong 已提交
74 75 76
  return 0;
}

L
Liu Jicong 已提交
77
int walChangeWrite(SWal* pWal, int64_t ver) {
L
Liu Jicong 已提交
78
  int     code = 0;
79
  TdFilePtr pIdxTFile, pLogTFile;
L
Liu Jicong 已提交
80
  char    fnameStr[WAL_FILE_LEN];
81 82
  if (pWal->pWriteLogTFile != NULL) {
    code = taosCloseFile(&pWal->pWriteLogTFile);
L
Liu Jicong 已提交
83 84 85 86
    if (code != 0) {
      terrno = TAOS_SYSTEM_ERROR(errno);
      return -1;
    }
L
Liu Jicong 已提交
87
  }
88 89
  if (pWal->pWriteIdxTFile != NULL) {
    code = taosCloseFile(&pWal->pWriteIdxTFile);
L
Liu Jicong 已提交
90 91 92 93
    if (code != 0) {
      terrno = TAOS_SYSTEM_ERROR(errno);
      return -1;
    }
L
Liu Jicong 已提交
94
  }
L
Liu Jicong 已提交
95

L
Liu Jicong 已提交
96
  SWalFileInfo tmpInfo;
L
Liu Jicong 已提交
97
  tmpInfo.firstVer = ver;
L
Liu Jicong 已提交
98
  // bsearch in fileSet
L
Liu Jicong 已提交
99 100 101 102 103 104 105
  int32_t idx = taosArraySearchIdx(pWal->fileInfoSet, &tmpInfo, compareWalFileInfo, TD_LE);
  ASSERT(idx != -1);
  SWalFileInfo* pFileInfo = taosArrayGet(pWal->fileInfoSet, idx);
  /*ASSERT(pFileInfo != NULL);*/

  int64_t fileFirstVer = pFileInfo->firstVer;
  walBuildIdxName(pWal, fileFirstVer, fnameStr);
106
  pIdxTFile = taosOpenFile(fnameStr, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_APPEND);
107
  if (pIdxTFile == NULL) {
L
Liu Jicong 已提交
108
    terrno = TAOS_SYSTEM_ERROR(errno);
109
    pWal->pWriteIdxTFile = NULL;
L
Liu Jicong 已提交
110 111 112
    return -1;
  }
  walBuildLogName(pWal, fileFirstVer, fnameStr);
113
  pLogTFile = taosOpenFile(fnameStr, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_APPEND);
114
  if (pLogTFile == NULL) {
115
    taosCloseFile(&pIdxTFile);
L
Liu Jicong 已提交
116
    terrno = TAOS_SYSTEM_ERROR(errno);
117
    pWal->pWriteLogTFile = NULL;
L
Liu Jicong 已提交
118
    return -1;
L
Liu Jicong 已提交
119 120
  }

121 122
  pWal->pWriteLogTFile = pLogTFile;
  pWal->pWriteIdxTFile = pIdxTFile;
L
Liu Jicong 已提交
123
  pWal->writeCur = idx;
L
Liu Jicong 已提交
124
  return fileFirstVer;
L
Liu Jicong 已提交
125 126
}

L
Liu Jicong 已提交
127
int walSeekWriteVer(SWal* pWal, int64_t ver) {
L
Liu Jicong 已提交
128
  int code;
L
Liu Jicong 已提交
129
  if (ver == pWal->vers.lastVer) {
L
Liu Jicong 已提交
130 131
    return 0;
  }
L
Liu Jicong 已提交
132
  if (ver > pWal->vers.lastVer || ver < pWal->vers.firstVer) {
L
Liu Jicong 已提交
133
    terrno = TSDB_CODE_WAL_INVALID_VER;
L
Liu Jicong 已提交
134 135
    return -1;
  }
L
Liu Jicong 已提交
136
  if (ver < pWal->vers.snapshotVer) {
L
Liu Jicong 已提交
137
    
L
Liu Jicong 已提交
138
  }
L
Liu Jicong 已提交
139
  if (ver < walGetCurFileFirstVer(pWal) || (ver > walGetCurFileLastVer(pWal))) {
L
Liu Jicong 已提交
140
    code = walChangeWrite(pWal, ver);
L
Liu Jicong 已提交
141
    if (code != 0) {
L
Liu Jicong 已提交
142 143
      return -1;
    }
L
Liu Jicong 已提交
144
  }
L
Liu Jicong 已提交
145
  code = walSeekWritePos(pWal, ver);
L
Liu Jicong 已提交
146
  if (code != 0) {
L
Liu Jicong 已提交
147 148
    return -1;
  }
L
Liu Jicong 已提交
149

L
Liu Jicong 已提交
150 151
  return 0;
}