-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
FailureStorage.java
185 lines (168 loc) · 5.46 KB
/
FailureStorage.java
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.api.impl.index.storage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.neo4j.helpers.UTF8;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.fs.StoreChannel;
import org.neo4j.kernel.api.impl.index.storage.layout.FolderLayout;
/**
* Helper class for storing a failure message that happens during an OutOfDisk situation in
* a pre-allocated file
*/
public class FailureStorage
{
private static final int MAX_FAILURE_SIZE = 16384;
private static final String DEFAULT_FAILURE_FILE_NAME = "failure-message";
private final FileSystemAbstraction fs;
private final FolderLayout folderLayout;
private final String failureFileName;
/**
* @param failureFileName name of failure files to be created
* @param folderLayout describing where failure files should be stored
*/
public FailureStorage( FileSystemAbstraction fs, FolderLayout folderLayout, String failureFileName )
{
this.fs = fs;
this.folderLayout = folderLayout;
this.failureFileName = failureFileName;
}
public FailureStorage( FileSystemAbstraction fs, FolderLayout folderLayout )
{
this( fs, folderLayout, DEFAULT_FAILURE_FILE_NAME );
}
/**
* Create/reserve an empty failure file for the given indexId.
*
* This will overwrite any pre-existing failure file.
*
* @throws IOException if the failure file could not be created
*/
public synchronized void reserveForIndex() throws IOException
{
fs.mkdirs( folderLayout.getIndexFolder() );
File failureFile = failureFile();
try ( StoreChannel channel = fs.create( failureFile ) )
{
channel.write( ByteBuffer.wrap( new byte[MAX_FAILURE_SIZE] ) );
channel.force( true );
channel.close();
}
}
/**
* Delete failure file for the given index id
*
*/
public synchronized void clearForIndex()
{
fs.deleteFile( failureFile() );
}
/**
* @return the failure, if any. Otherwise {@code null} marking no failure.
*/
public synchronized String loadIndexFailure()
{
File failureFile = failureFile();
try
{
if ( !fs.fileExists( failureFile ) || !isFailed( failureFile ) )
{
return null;
}
return readFailure( failureFile );
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}
/**
* Store failure in failure file for index with the given id
*
* @param failure message describing the failure that needs to be stored
* @throws IOException if the failure could not be stored
*/
public synchronized void storeIndexFailure( String failure ) throws IOException
{
File failureFile = failureFile();
try ( StoreChannel channel = fs.open( failureFile, "rw" ) )
{
byte[] data = UTF8.encode( failure );
channel.write( ByteBuffer.wrap( data, 0, Math.min( data.length, MAX_FAILURE_SIZE ) ) );
channel.force( true );
channel.close();
}
}
File failureFile()
{
File folder = folderLayout.getIndexFolder();
return new File( folder, failureFileName );
}
private String readFailure( File failureFile ) throws IOException
{
try ( StoreChannel channel = fs.open( failureFile, "r" ) )
{
byte[] data = new byte[(int) channel.size()];
int readData = channel.read( ByteBuffer.wrap( data ) );
channel.close();
return readData <= 0 ? "" : UTF8.decode( withoutZeros( data ) );
}
}
private static byte[] withoutZeros( byte[] data )
{
byte[] result = new byte[ lengthOf(data) ];
System.arraycopy( data, 0, result, 0, result.length );
return result;
}
private static int lengthOf( byte[] data )
{
for (int i = 0; i < data.length; i++ )
{
if ( 0 == data[i] )
{
return i;
}
}
return data.length;
}
private boolean isFailed( File failureFile ) throws IOException
{
try ( StoreChannel channel = fs.open( failureFile, "r" ) )
{
byte[] data = new byte[(int) channel.size()];
channel.read( ByteBuffer.wrap( data ) );
channel.close();
return !allZero( data );
}
}
private static boolean allZero( byte[] data )
{
for ( byte b : data )
{
if ( b != 0 )
{
return false;
}
}
return true;
}
}