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
/*
 * 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
static int walSeekWritePos(SWal* pWal, int64_t ver) {
L
Liu Jicong 已提交
23
  int code = 0;
L
Liu Jicong 已提交
24

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

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

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

  char fnameStr[WAL_FILE_LEN];
  walBuildIdxName(pWal, fileFirstVer, fnameStr);
59
  pIdxTFile = taosOpenFile(fnameStr, TD_FILE_CTEATE | 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_CTEATE | 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
71 72
  pWal->pWriteIdxTFile = pIdxTFile;
  pWal->pWriteLogTFile = pLogTFile;
L
Liu Jicong 已提交
73 74 75
  return 0;
}

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

L
Liu Jicong 已提交
95
  SWalFileInfo tmpInfo;
L
Liu Jicong 已提交
96
  tmpInfo.firstVer = ver;
L
Liu Jicong 已提交
97
  // bsearch in fileSet
L
Liu Jicong 已提交
98 99 100 101 102 103 104
  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);
105
  pIdxTFile = taosOpenFile(fnameStr, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_APPEND);
106
  if (pIdxTFile == NULL) {
L
Liu Jicong 已提交
107
    terrno = TAOS_SYSTEM_ERROR(errno);
108
    pWal->pWriteIdxTFile = NULL;
L
Liu Jicong 已提交
109 110 111
    return -1;
  }
  walBuildLogName(pWal, fileFirstVer, fnameStr);
112
  pLogTFile = taosOpenFile(fnameStr, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_APPEND);
113
  if (pLogTFile == NULL) {
114
    taosCloseFile(&pIdxTFile);
L
Liu Jicong 已提交
115
    terrno = TAOS_SYSTEM_ERROR(errno);
116
    pWal->pWriteLogTFile = NULL;
L
Liu Jicong 已提交
117
    return -1;
L
Liu Jicong 已提交
118 119
  }

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

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

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