| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.store.fs.retry; |
| 7 |
|
| 8 |
import java.io.IOException; |
| 9 |
import java.nio.ByteBuffer; |
| 10 |
import java.nio.channels.ClosedByInterruptException; |
| 11 |
import java.nio.channels.ClosedChannelException; |
| 12 |
import java.nio.channels.FileChannel; |
| 13 |
import java.nio.channels.FileLock; |
| 14 |
import org.h2.store.fs.FileBase; |
| 15 |
import org.h2.store.fs.FileUtils; |
| 16 |
|
| 17 |
/** |
| 18 |
* A file object that re-opens and re-tries the operation if the file was |
| 19 |
* closed. |
| 20 |
*/ |
| 21 |
class FileRetryOnInterrupt extends FileBase { |
| 22 |
|
| 23 |
private final String fileName; |
| 24 |
private final String mode; |
| 25 |
private FileChannel channel; |
| 26 |
private FileLockRetry lock; |
| 27 |
|
| 28 |
FileRetryOnInterrupt(String fileName, String mode) throws IOException { |
| 29 |
this.fileName = fileName; |
| 30 |
this.mode = mode; |
| 31 |
open(); |
| 32 |
} |
| 33 |
|
| 34 |
private void open() throws IOException { |
| 35 |
channel = FileUtils.open(fileName, mode); |
| 36 |
} |
| 37 |
|
| 38 |
private void reopen(int i, IOException e) throws IOException { |
| 39 |
if (i > 20) { |
| 40 |
throw e; |
| 41 |
} |
| 42 |
if (!(e instanceof ClosedByInterruptException) && |
| 43 |
!(e instanceof ClosedChannelException)) { |
| 44 |
throw e; |
| 45 |
} |
| 46 |
// clear the interrupt flag, to avoid re-opening many times |
| 47 |
Thread.interrupted(); |
| 48 |
FileChannel before = channel; |
| 49 |
// ensure we don't re-open concurrently; |
| 50 |
// sometimes we don't re-open, which is fine, |
| 51 |
// as this method is called in a loop |
| 52 |
synchronized (this) { |
| 53 |
if (before == channel) { |
| 54 |
open(); |
| 55 |
reLock(); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
private void reLock() throws IOException { |
| 61 |
if (lock == null) { |
| 62 |
return; |
| 63 |
} |
| 64 |
try { |
| 65 |
lock.base.release(); |
| 66 |
} catch (IOException e) { |
| 67 |
// ignore |
| 68 |
} |
| 69 |
FileLock l2 = channel.tryLock(lock.position(), lock.size(), lock.isShared()); |
| 70 |
if (l2 == null) { |
| 71 |
throw new IOException("Re-locking failed"); |
| 72 |
} |
| 73 |
lock.base = l2; |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public void implCloseChannel() throws IOException { |
| 78 |
try { |
| 79 |
channel.close(); |
| 80 |
} catch (IOException e) { |
| 81 |
// ignore |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public long position() throws IOException { |
| 87 |
for (int i = 0;; i++) { |
| 88 |
try { |
| 89 |
return channel.position(); |
| 90 |
} catch (IOException e) { |
| 91 |
reopen(i, e); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public long size() throws IOException { |
| 98 |
for (int i = 0;; i++) { |
| 99 |
try { |
| 100 |
return channel.size(); |
| 101 |
} catch (IOException e) { |
| 102 |
reopen(i, e); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public int read(ByteBuffer dst) throws IOException { |
| 109 |
long pos = position(); |
| 110 |
for (int i = 0;; i++) { |
| 111 |
try { |
| 112 |
return channel.read(dst); |
| 113 |
} catch (IOException e) { |
| 114 |
reopen(i, e); |
| 115 |
position(pos); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public int read(ByteBuffer dst, long position) throws IOException { |
| 122 |
for (int i = 0;; i++) { |
| 123 |
try { |
| 124 |
return channel.read(dst, position); |
| 125 |
} catch (IOException e) { |
| 126 |
reopen(i, e); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public FileChannel position(long pos) throws IOException { |
| 133 |
for (int i = 0;; i++) { |
| 134 |
try { |
| 135 |
channel.position(pos); |
| 136 |
return this; |
| 137 |
} catch (IOException e) { |
| 138 |
reopen(i, e); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
public FileChannel truncate(long newLength) throws IOException { |
| 145 |
for (int i = 0;; i++) { |
| 146 |
try { |
| 147 |
channel.truncate(newLength); |
| 148 |
return this; |
| 149 |
} catch (IOException e) { |
| 150 |
reopen(i, e); |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public void force(boolean metaData) throws IOException { |
| 157 |
for (int i = 0;; i++) { |
| 158 |
try { |
| 159 |
channel.force(metaData); |
| 160 |
return; |
| 161 |
} catch (IOException e) { |
| 162 |
reopen(i, e); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
@Override |
| 168 |
public int write(ByteBuffer src) throws IOException { |
| 169 |
long pos = position(); |
| 170 |
for (int i = 0;; i++) { |
| 171 |
try { |
| 172 |
return channel.write(src); |
| 173 |
} catch (IOException e) { |
| 174 |
reopen(i, e); |
| 175 |
position(pos); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
@Override |
| 181 |
public int write(ByteBuffer src, long position) throws IOException { |
| 182 |
for (int i = 0;; i++) { |
| 183 |
try { |
| 184 |
return channel.write(src, position); |
| 185 |
} catch (IOException e) { |
| 186 |
reopen(i, e); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
@Override |
| 192 |
public synchronized FileLock tryLock(long position, long size, |
| 193 |
boolean shared) throws IOException { |
| 194 |
FileLock l = channel.tryLock(position, size, shared); |
| 195 |
if (l == null) { |
| 196 |
return null; |
| 197 |
} |
| 198 |
lock = new FileLockRetry(l, this); |
| 199 |
return lock; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* A wrapped file lock. |
| 204 |
*/ |
| 205 |
static class FileLockRetry extends FileLock { |
| 206 |
|
| 207 |
/** |
| 208 |
* The base lock. |
| 209 |
*/ |
| 210 |
FileLock base; |
| 211 |
|
| 212 |
protected FileLockRetry(FileLock base, FileChannel channel) { |
| 213 |
super(channel, base.position(), base.size(), base.isShared()); |
| 214 |
this.base = base; |
| 215 |
} |
| 216 |
|
| 217 |
@Override |
| 218 |
public boolean isValid() { |
| 219 |
return base.isValid(); |
| 220 |
} |
| 221 |
|
| 222 |
@Override |
| 223 |
public void release() throws IOException { |
| 224 |
base.release(); |
| 225 |
} |
| 226 |
|
| 227 |
} |
| 228 |
|
| 229 |
@Override |
| 230 |
public String toString() { |
| 231 |
return FilePathRetryOnInterrupt.SCHEME + ":" + fileName; |
| 232 |
} |
| 233 |
|
| 234 |
} |