walSeek.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
/*
 * 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"
L
Liu Jicong 已提交
19
#include "tref.h"
L
Liu Jicong 已提交
20 21
#include "walInt.h"

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

L
Liu Jicong 已提交
26 27
  TdFilePtr pIdxTFile = pWal->pIdxFile;
  TdFilePtr pLogTFile = pWal->pLogFile;
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
  }
43
  code = taosLSeekFile(pLogTFile, entry.offset, SEEK_SET);
L
Liu Jicong 已提交
44
  if (code < 0) {
L
Liu Jicong 已提交
45
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
46
    return -1;
L
Liu Jicong 已提交
47
  }
L
Liu Jicong 已提交
48
  return 0;
L
Liu Jicong 已提交
49
}
L
Liu Jicong 已提交
50
#endif
L
Liu Jicong 已提交
51

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

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

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

L
Liu Jicong 已提交
106
  SWalFileInfo tmpInfo;
L
Liu Jicong 已提交
107
  tmpInfo.firstVer = ver;
L
Liu Jicong 已提交
108
  // bsearch in fileSet
L
Liu Jicong 已提交
109
  int32_t idx = taosArraySearchIdx(pWal->fileInfoSet, &tmpInfo, compareWalFileInfo, TD_LE);
L
Liu Jicong 已提交
110
  /*A(idx != -1);*/
L
Liu Jicong 已提交
111 112 113 114
  SWalFileInfo* pFileInfo = taosArrayGet(pWal->fileInfoSet, idx);

  int64_t fileFirstVer = pFileInfo->firstVer;
  walBuildIdxName(pWal, fileFirstVer, fnameStr);
115
  pIdxTFile = taosOpenFile(fnameStr, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND);
116
  if (pIdxTFile == NULL) {
L
Liu Jicong 已提交
117
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
118
    pWal->pIdxFile = NULL;
L
Liu Jicong 已提交
119 120 121
    return -1;
  }
  walBuildLogName(pWal, fileFirstVer, fnameStr);
122
  pLogTFile = taosOpenFile(fnameStr, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND);
123
  if (pLogTFile == NULL) {
124
    taosCloseFile(&pIdxTFile);
L
Liu Jicong 已提交
125
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
126
    pWal->pLogFile = NULL;
L
Liu Jicong 已提交
127
    return -1;
L
Liu Jicong 已提交
128 129
  }

L
Liu Jicong 已提交
130 131
  pWal->pLogFile = pLogTFile;
  pWal->pIdxFile = pIdxTFile;
L
Liu Jicong 已提交
132
  pWal->writeCur = idx;
L
Liu Jicong 已提交
133
  return fileFirstVer;
L
Liu Jicong 已提交
134 135
}

L
Liu Jicong 已提交
136
#if 0
L
Liu Jicong 已提交
137
int walSeekWriteVer(SWal* pWal, int64_t ver) {
L
Liu Jicong 已提交
138
  int64_t code;
L
Liu Jicong 已提交
139
  if (ver == pWal->vers.lastVer) {
L
Liu Jicong 已提交
140 141
    return 0;
  }
L
Liu Jicong 已提交
142
  if (ver > pWal->vers.lastVer || ver < pWal->vers.firstVer) {
L
Liu Jicong 已提交
143
    terrno = TSDB_CODE_WAL_INVALID_VER;
L
Liu Jicong 已提交
144 145
    return -1;
  }
L
Liu Jicong 已提交
146
  if (ver < pWal->vers.snapshotVer) {
L
Liu Jicong 已提交
147
  }
L
Liu Jicong 已提交
148
  if (ver < walGetCurFileFirstVer(pWal) || (ver > walGetCurFileLastVer(pWal))) {
L
Liu Jicong 已提交
149
    code = walChangeWrite(pWal, ver);
L
Liu Jicong 已提交
150
    if (code != 0) {
L
Liu Jicong 已提交
151 152
      return -1;
    }
L
Liu Jicong 已提交
153
  }
L
Liu Jicong 已提交
154
  code = walSeekWritePos(pWal, ver);
L
Liu Jicong 已提交
155
  if (code != 0) {
L
Liu Jicong 已提交
156 157
    return -1;
  }
L
Liu Jicong 已提交
158

L
Liu Jicong 已提交
159 160
  return 0;
}
L
Liu Jicong 已提交
161
#endif