SequenceManager.java 9.04 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
6
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
Matt Tucker's avatar
Matt Tucker committed
19 20 21 22 23 24 25 26
 */

package org.jivesoftware.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
27
import java.util.Map;
28
import java.util.concurrent.ConcurrentHashMap;
Matt Tucker's avatar
Matt Tucker committed
29

30 31 32 33
import org.jivesoftware.util.JiveConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Matt Tucker's avatar
Matt Tucker committed
34 35 36
/**
 * Manages sequences of unique ID's that get stored in the database. Database support for sequences
 * varies widely; some don't use them at all. Instead, we handle unique ID generation with a
37 38
 * combination VM/database solution.
 * <p>
Matt Tucker's avatar
Matt Tucker committed
39
 * A special table in the database doles out blocks of unique ID's to each
40
 * virtual machine that interacts with Jive. This has the following consequences:</p>
Matt Tucker's avatar
Matt Tucker committed
41 42 43 44 45 46
 * <ul>
 * <li>There is no need to go to the database every time we want a new unique id.
 * <li>Multiple app servers can interact with the same db without id collision.
 * <li>The order of unique id's may not correspond to the creation date of objects.
 * <li>There can be gaps in ID's after server restarts since blocks will get "lost" if the block
 * size is greater than 1.
47
 * </ul>
Matt Tucker's avatar
Matt Tucker committed
48 49 50 51 52 53 54 55 56
 * Each sequence type that this class manages has a different block size value. Objects that aren't
 * created often have a block size of 1, while frequently created objects such as entries and
 * comments have larger block sizes.
 *
 * @author Matt Tucker
 * @author Bruce Ritchie
 */
public class SequenceManager {

57 58
	private static final Logger Log = LoggerFactory.getLogger(SequenceManager.class);

59
    private static final String CREATE_ID =
60
            "INSERT INTO ofID (id, idType) VALUES (1, ?)";
61

Matt Tucker's avatar
Matt Tucker committed
62
    private static final String LOAD_ID =
63
            "SELECT id FROM ofID WHERE idType=?";
64

Matt Tucker's avatar
Matt Tucker committed
65
    private static final String UPDATE_ID =
66
            "UPDATE ofID SET id=? WHERE idType=? AND id=?";
Matt Tucker's avatar
Matt Tucker committed
67 68

    // Statically startup a sequence manager for each of the sequence counters.
69
    private static Map<Integer, SequenceManager> managers = new ConcurrentHashMap<>();
Matt Tucker's avatar
Matt Tucker committed
70 71 72

    static {
        new SequenceManager(JiveConstants.ROSTER, 5);
73 74
        new SequenceManager(JiveConstants.OFFLINE, 5);
        new SequenceManager(JiveConstants.MUC_ROOM, 5);
75
    }
Matt Tucker's avatar
Matt Tucker committed
76 77 78 79 80 81 82 83 84

    /**
     * Returns the next ID of the specified type.
     *
     * @param type the type of unique ID.
     * @return the next unique ID of the specified type.
     */
    public static long nextID(int type) {
        if (managers.containsKey(type)) {
85
            return managers.get(type).nextUniqueID();
Matt Tucker's avatar
Matt Tucker committed
86 87
        }
        else {
88 89
            // Verify type is valid from the db, if so create an instance for the type
            // And return the next unique id
90 91
            SequenceManager manager = new SequenceManager(type, 1);
            return manager.nextUniqueID();
Matt Tucker's avatar
Matt Tucker committed
92 93 94
        }
    }

95
    /**
Andrew Wright's avatar
Andrew Wright committed
96
     * Returns the next id for an object that has defined the annotation {@link JiveID}.
97
     * The JiveID annotation value is the synonymous for the type integer.<p>
Matt Tucker's avatar
Matt Tucker committed
98
     *
99
     * The annotation JiveID should contain the id type for the object (the same number you would
100
     * use to call nextID(int type)). Example class definition:</p>
101 102 103
     * <code>
     * \@JiveID(10)
     * public class MyClass {
Matt Tucker's avatar
Matt Tucker committed
104
     *
105 106 107
     * }
     * </code>
     *
Matt Tucker's avatar
Matt Tucker committed
108 109
     * @param o object that has annotation JiveID.
     * @return the next unique ID.
Andrew Wright's avatar
Andrew Wright committed
110
     * @throws IllegalArgumentException If the object passed in does not defined {@link JiveID}
111 112 113 114
     */
    public static long nextID(Object o) {
        JiveID id = o.getClass().getAnnotation(JiveID.class);

115 116
        if (id == null) {
            Log.error("Annotation JiveID must be defined in the class " + o.getClass());
Andrew Wright's avatar
Andrew Wright committed
117
            throw new IllegalArgumentException(
118
                    "Annotation JiveID must be defined in the class " + o.getClass());
119 120 121 122 123
        }

        return nextID(id.value());
    }

124
    /**
Matt Tucker's avatar
Matt Tucker committed
125 126 127
     * Used to set the blocksize of a given SequenceManager. If no SequenceManager has
     * been registered for the type, the type is verified as valid and then a new
     * sequence manager is created.
128
     *
Matt Tucker's avatar
Matt Tucker committed
129 130
     * @param type the type of unique id.
     * @param blockSize how many blocks of ids we should.
131 132 133 134 135 136
     */
    public static void setBlockSize(int type, int blockSize) {
        if (managers.containsKey(type)) {
            managers.get(type).blockSize = blockSize;
        }
        else {
137
            new SequenceManager(type, blockSize);
138 139 140
        }
    }

Matt Tucker's avatar
Matt Tucker committed
141 142 143 144 145 146 147 148 149
    private int type;
    private long currentID;
    private long maxID;
    private int blockSize;

    /**
     * Creates a new DbSequenceManager.
     *
     * @param seqType the type of sequence.
Matt Tucker's avatar
Matt Tucker committed
150
     * @param size the number of id's to "checkout" at a time.
Matt Tucker's avatar
Matt Tucker committed
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 186 187 188 189 190 191
     */
    public SequenceManager(int seqType, int size) {
        managers.put(seqType, this);
        this.type = seqType;
        this.blockSize = size;
        currentID = 0l;
        maxID = 0l;
    }

    /**
     * Returns the next available unique ID. Essentially this provides for the functionality of an
     * auto-increment database field.
     */
    public synchronized long nextUniqueID() {
        if (!(currentID < maxID)) {
            // Get next block -- make 5 attempts at maximum.
            getNextBlock(5);
        }
        long id = currentID;
        currentID++;
        return id;
    }

    /**
     * Performs a lookup to get the next available ID block. The algorithm is as follows:
     * <ol>
     * <li> Select currentID from appropriate db row.
     * <li> Increment id returned from db.
     * <li> Update db row with new id where id=old_id.
     * <li> If update fails another process checked out the block first; go back to step 1.
     * Otherwise, done.
     * </ol>
     */
    private void getNextBlock(int count) {
        if (count == 0) {
            Log.error("Failed at last attempt to obtain an ID, aborting...");
            return;
        }

        Connection con = null;
        PreparedStatement pstmt = null;
192
        ResultSet rs = null;
Matt Tucker's avatar
Matt Tucker committed
193 194 195 196 197 198 199 200
        boolean abortTransaction = false;
        boolean success = false;

        try {
            con = DbConnectionManager.getTransactionConnection();
            // Get the current ID from the database.
            pstmt = con.prepareStatement(LOAD_ID);
            pstmt.setInt(1, type);
201
            rs = pstmt.executeQuery();
202 203

            long currentID = 1;
204 205
            if (rs.next()) {
                currentID = rs.getLong(1);
206 207
            }
            else {
208
                createNewID(con, type);
Matt Tucker's avatar
Matt Tucker committed
209
            }
210
            DbConnectionManager.fastcloseStmt(rs, pstmt);
Matt Tucker's avatar
Matt Tucker committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

            // Increment the id to define our block.
            long newID = currentID + blockSize;
            // The WHERE clause includes the last value of the id. This ensures
            // that an update will occur only if nobody else has performed an
            // update first.
            pstmt = con.prepareStatement(UPDATE_ID);
            pstmt.setLong(1, newID);
            pstmt.setInt(2, type);
            pstmt.setLong(3, currentID);
            // Check to see if the row was affected. If not, some other process
            // already changed the original id that we read. Therefore, this
            // round failed and we'll have to try again.
            success = pstmt.executeUpdate() == 1;
            if (success) {
                this.currentID = currentID;
                this.maxID = newID;
            }
        }
        catch (SQLException e) {
231
            Log.error(e.getMessage(), e);
Matt Tucker's avatar
Matt Tucker committed
232 233 234
            abortTransaction = true;
        }
        finally {
235
            DbConnectionManager.closeStatement(rs, pstmt);
Matt Tucker's avatar
Matt Tucker committed
236 237 238 239
            DbConnectionManager.closeTransactionConnection(con, abortTransaction);
        }

        if (!success) {
240
            Log.warn("WARNING: failed to obtain next ID block due to " +
Matt Tucker's avatar
Matt Tucker committed
241 242 243 244 245 246
                    "thread contention. Trying again...");
            // Call this method again, but sleep briefly to try to avoid thread contention.
            try {
                Thread.sleep(75);
            }
            catch (InterruptedException ie) {
Matt Tucker's avatar
Matt Tucker committed
247
                // Ignore.
Matt Tucker's avatar
Matt Tucker committed
248 249 250 251
            }
            getNextBlock(count - 1);
        }
    }
252

253 254
    private void createNewID(Connection con, int type) throws SQLException {
        Log.warn("Autocreating jiveID row for type '" + type + "'");
255

256
        // create new ID row
257
        PreparedStatement pstmt = null;
258

259
        try {
260
            pstmt = con.prepareStatement(CREATE_ID);
261
            pstmt.setInt(1, type);
262
            pstmt.execute();
263 264
        }
        finally {
265
            DbConnectionManager.closeStatement(pstmt);
266 267
        }
    }
Matt Tucker's avatar
Matt Tucker committed
268
}