forked from Graylog2/graylog2-server
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lennart Koopmann
committed
Sep 6, 2012
1 parent
ff53563
commit 13d04dd
Showing
12 changed files
with
457 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/** | ||
* Copyright 2012 Lennart Koopmann <lennart@socketfeed.com> | ||
* | ||
* This file is part of Graylog2. | ||
* | ||
* Graylog2 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. | ||
* | ||
* Graylog2 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 Graylog2. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
package org.graylog2.indexer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.logging.Level; | ||
import org.apache.log4j.Logger; | ||
import org.elasticsearch.action.admin.indices.stats.IndexStats; | ||
import org.graylog2.GraylogServer; | ||
|
||
/** | ||
* | ||
* Format of actual indexes behind the Deflector: | ||
* [configured_prefix]_1 | ||
* [configured_prefix]_2 | ||
* [configured_prefix]_3 | ||
* ... | ||
* | ||
* @author Lennart Koopmann <lennart@socketfeed.com> | ||
*/ | ||
public class Deflector { | ||
|
||
private static final Logger LOG = Logger.getLogger(Deflector.class); | ||
|
||
public static final String DEFLECTOR_NAME = "graylog2_deflector"; | ||
|
||
GraylogServer server; | ||
|
||
public Deflector(GraylogServer server) { | ||
this.server = server; | ||
} | ||
|
||
public boolean isUp() { | ||
return this.server.getIndexer().indexExists(DEFLECTOR_NAME); | ||
} | ||
|
||
public void setUp() { | ||
// Check if there already is an deflector index pointing somewhere. | ||
if (isUp()) { | ||
LOG.info("Found deflector alias <" + DEFLECTOR_NAME + ">. Using it."); | ||
} else { | ||
LOG.info("Did not find an deflector alias. Setting one up now."); | ||
|
||
// Do we have a target index to point to? | ||
try { | ||
String currentTarget = getCurrentTargetName(); | ||
LOG.info("Pointing to already existing index target <" + currentTarget + ">"); | ||
pointTo(currentTarget); | ||
} catch(NoTargetIndexException ex) { | ||
LOG.info("There is no index target to point to. Creating one now."); | ||
cycle(); // No index, so automatically cycling to 0. | ||
} | ||
} | ||
} | ||
|
||
public void cycle() { | ||
LOG.info("Cycling deflector to next index now."); | ||
int oldTargetNumber; | ||
|
||
try { | ||
oldTargetNumber = getCurrentTargetNumber(); | ||
} catch (NoTargetIndexException ex) { | ||
oldTargetNumber = -1; | ||
} | ||
|
||
int newTargetNumber = oldTargetNumber+1; | ||
|
||
String newTarget = buildIndexName(server.getConfiguration().getElasticSearchIndexPrefix(), newTargetNumber); | ||
String oldTarget = buildIndexName(server.getConfiguration().getElasticSearchIndexPrefix(), oldTargetNumber); | ||
|
||
LOG.info("Cycling from <" + oldTarget + "> to <" + newTarget + ">"); | ||
|
||
// Create new index. | ||
LOG.info("Creating index target <" + newTarget + ">..."); | ||
server.getIndexer().createIndex(newTarget); | ||
LOG.info("Done!"); | ||
|
||
// Point to deflector to new index. | ||
LOG.info("Pointing deflector to new target index...."); | ||
if (oldTargetNumber == -1) { | ||
// Only poiting, not cycling. | ||
pointTo(newTarget); | ||
} else { | ||
// Re-poiting from existing old index to the new one. | ||
pointTo(newTarget, oldTarget); | ||
} | ||
LOG.info("Done!"); | ||
} | ||
|
||
public int getCurrentTargetNumber() throws NoTargetIndexException { | ||
Map<String, IndexStats> indexes = this.server.getIndexer().getIndices(); | ||
if (indexes.isEmpty()) { | ||
throw new NoTargetIndexException(); | ||
} | ||
|
||
List<Integer> indexNumbers = new ArrayList<Integer>(); | ||
|
||
for(Map.Entry<String, IndexStats> e : indexes.entrySet()) { | ||
try { | ||
indexNumbers.add(extractIndexNumber(e.getKey())); | ||
} catch (NumberFormatException ex) { | ||
continue; | ||
} | ||
} | ||
|
||
if (indexNumbers.isEmpty()) { | ||
throw new NoTargetIndexException(); | ||
} | ||
|
||
return Collections.max(indexNumbers); | ||
} | ||
|
||
public String getCurrentTargetName() throws NoTargetIndexException { | ||
return buildIndexName(this.server.getConfiguration().getElasticSearchIndexPrefix(), getCurrentTargetNumber()); | ||
} | ||
|
||
public String buildIndexName(String prefix, int number) { | ||
return prefix + "_" + number; | ||
} | ||
|
||
public int extractIndexNumber(String indexName) throws NumberFormatException { | ||
String[] parts = indexName.split("_"); | ||
|
||
try { | ||
return Integer.parseInt(parts[parts.length-1]); | ||
} catch(Exception e) { | ||
LOG.debug("Could not extract index number from index <" + indexName + ">."); | ||
throw new NumberFormatException(); | ||
} | ||
} | ||
|
||
private void pointTo(String newIndex, String oldIndex) { | ||
server.getIndexer().cycleAlias(DEFLECTOR_NAME, newIndex, oldIndex); | ||
} | ||
|
||
private void pointTo(String newIndex) { | ||
server.getIndexer().cycleAlias(DEFLECTOR_NAME, newIndex); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.