/src/main/java/org/graylog2/database/MongoConnection.java

https://github.com/backupify/graylog2-server · Java · 184 lines · 98 code · 25 blank · 61 comment · 16 complexity · e4889d6935ef5adb8f3ddb421fa4b6ac MD5 · raw file

  1. /**
  2. * Copyright 2010, 2011 Lennart Koopmann <lennart@socketfeed.com>
  3. *
  4. * This file is part of Graylog2.
  5. *
  6. * Graylog2 is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Graylog2 is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Graylog2. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package org.graylog2.database;
  21. import com.mongodb.BasicDBObject;
  22. import com.mongodb.BasicDBObjectBuilder;
  23. import com.mongodb.Mongo;
  24. import com.mongodb.DB;
  25. import com.mongodb.DBCollection;
  26. import com.mongodb.MongoException;
  27. import com.mongodb.MongoOptions;
  28. import com.mongodb.ServerAddress;
  29. import java.util.List;
  30. import org.graylog2.Configuration;
  31. import org.graylog2.Main;
  32. /**
  33. * MongoConnection.java: Jun 6, 2010 1:36:19 PM
  34. *
  35. * MongoDB connection singleton
  36. *
  37. * @author: Lennart Koopmann <lennart@socketfeed.com>
  38. */
  39. public final class MongoConnection {
  40. private static MongoConnection instance;
  41. private Mongo m = null;
  42. private DB db = null;
  43. private DBCollection messagesCollection = null;
  44. private DBCollection historicServerValuesCollection = null;
  45. private MongoConnection() {}
  46. /**
  47. * Get the connection instance
  48. * @return MongoConnection instance
  49. */
  50. public synchronized static MongoConnection getInstance() {
  51. if (instance == null) {
  52. instance = new MongoConnection();
  53. }
  54. return instance;
  55. }
  56. /**
  57. * Connect the instance.
  58. *
  59. * @param username MongoDB user
  60. * @param password MongoDB password
  61. * @param hostname MongoDB host
  62. * @param database MongoDB database
  63. * @param port MongoDB port
  64. * @param useAuth Use authentication?
  65. * @throws Exception
  66. */
  67. public void connect(String username, String password, String hostname, String database, int port, String useAuth, List<ServerAddress> replicaServers) throws Exception {
  68. try {
  69. MongoOptions options = new MongoOptions();
  70. options.connectionsPerHost = Configuration.getMaximumMongoDBConnections(Main.masterConfig);
  71. options.threadsAllowedToBlockForConnectionMultiplier = Configuration.getThreadsAllowedToBlockMultiplier(Main.masterConfig);
  72. // Connect to replica servers if given. Else the standard way to one server.
  73. if (replicaServers != null && replicaServers.size() > 0) {
  74. m = new Mongo(replicaServers, options);
  75. } else {
  76. ServerAddress address = new ServerAddress(hostname, port);
  77. m = new Mongo(address, options);
  78. }
  79. db = m.getDB(database);
  80. // Try to authenticate if configured.
  81. if (useAuth.equals("true")) {
  82. if(!db.authenticate(username, password.toCharArray())) {
  83. throw new Exception("Could not authenticate to database '" + database + "' with user '" + username + "'.");
  84. }
  85. }
  86. } catch (MongoException.Network e) {
  87. throw new Exception("Could not connect to Mongo DB. (" + e.toString() + ")");
  88. }
  89. }
  90. /**
  91. * Returns the raw connection.
  92. * @return connection
  93. */
  94. public Mongo getConnection() {
  95. return m;
  96. }
  97. /**
  98. * Returns the raw database object.
  99. * @return database
  100. */
  101. public DB getDatabase() {
  102. return db;
  103. }
  104. /**
  105. * Get the messages collection. Lazily creates a new, capped one based on the
  106. * messages_collection_size from graylog2.conf if there is none.
  107. *
  108. * @return The messages collection
  109. */
  110. public DBCollection getMessagesColl() {
  111. if (this.messagesCollection != null) {
  112. return this.messagesCollection;
  113. }
  114. // Collection has not been cached yet. Do it now.
  115. DBCollection coll = null;
  116. // Create a capped collection if the collection does not yet exist.
  117. if(MongoConnection.getInstance().getDatabase().collectionExists("messages")) {
  118. coll = MongoConnection.getInstance().getDatabase().getCollection("messages");
  119. } else {
  120. long messagesCollSize = Long.parseLong(Main.masterConfig.getProperty("messages_collection_size").trim());
  121. coll = MongoConnection.getInstance()
  122. .getDatabase()
  123. .createCollection("messages", BasicDBObjectBuilder.start()
  124. .add("capped", true)
  125. .add("size", messagesCollSize)
  126. .get());
  127. }
  128. coll.ensureIndex(new BasicDBObject("_id", 1));
  129. coll.ensureIndex(new BasicDBObject("created_at", 1));
  130. coll.ensureIndex(new BasicDBObject("host", 1));
  131. coll.ensureIndex(new BasicDBObject("streams", 1));
  132. coll.ensureIndex(new BasicDBObject("facility", 1));
  133. coll.ensureIndex(new BasicDBObject("level", 1));
  134. this.messagesCollection = coll;
  135. return coll;
  136. }
  137. public DBCollection getHistoricServerValuesColl() {
  138. if (this.historicServerValuesCollection != null) {
  139. return this.historicServerValuesCollection;
  140. }
  141. // Collection has not been cached yet. Do it now.
  142. DBCollection coll = null;
  143. // Create a capped collection if the collection does not yet exist.
  144. if(MongoConnection.getInstance().getDatabase().collectionExists("historic_server_values")) {
  145. coll = MongoConnection.getInstance().getDatabase().getCollection("historic_server_values");
  146. } else {
  147. coll = MongoConnection.getInstance()
  148. .getDatabase().createCollection("historic_server_values", BasicDBObjectBuilder.start()
  149. .add("capped", true)
  150. .add("size", 10485760) // 10 MB
  151. .add("max", 720) // Minutes. -> 12 hours.
  152. .get());
  153. }
  154. coll.ensureIndex(new BasicDBObject("type", 1));
  155. coll.ensureIndex(new BasicDBObject("created_at", 1));
  156. this.historicServerValuesCollection = coll;
  157. return coll;
  158. }
  159. }