Skip to content

Commit

Permalink
Merge pull request #1344 from uronce-cc/wip_master_client_rawtable
Browse files Browse the repository at this point in the history
[TACHYON-820] Meta for the new RawTableMaster
  • Loading branch information
gpang committed Aug 21, 2015
2 parents e2eb282 + 8c196f9 commit 9551cde
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 2 deletions.
Expand Up @@ -20,5 +20,6 @@
// TODO
public interface JournalFormatter {
void serialize(JournalEntry entry, OutputStream outputStream);

void deserialize();
}
Expand Up @@ -15,6 +15,7 @@

package tachyon.master.next.rawtable;

import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.List;

Expand All @@ -23,14 +24,26 @@
import org.slf4j.LoggerFactory;

import tachyon.Constants;
import tachyon.conf.TachyonConf;
import tachyon.master.next.Master;
import tachyon.master.next.PeriodicTask;
import tachyon.master.next.filesystem.FileSystemMaster;
import tachyon.master.next.rawtable.meta.RawTables;
import tachyon.thrift.TachyonException;

// TODO: implement this master, but should be deprecated.
public class RawTableMaster implements Master {
private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);

public RawTableMaster() {
private final TachyonConf mTachyonConf;
private final long mMaxTableMetadataBytes;

private final FileSystemMaster mFileSystemMaster;
private final RawTables mRawTables = new RawTables();

public RawTableMaster(TachyonConf tachyonConf, FileSystemMaster fileSystemMaster) {
mTachyonConf = tachyonConf;
mMaxTableMetadataBytes = mTachyonConf.getBytes(Constants.MAX_TABLE_METADATA_BYTE);
mFileSystemMaster = fileSystemMaster;
}

@Override
Expand All @@ -47,4 +60,17 @@ public List<PeriodicTask> getPeriodicTaskList() {
// TODO
return Collections.emptyList();
}

/**
* Validate size of metadata is smaller than the configured maximum size. This should be called
* whenever a metadata wants to be set.
*
* @param metadata the metadata to be validated
* @throws TachyonException if the metadata is too large
*/
private void validateMetadataSize(ByteBuffer metadata) throws TachyonException {
if (metadata.limit() - metadata.position() >= mMaxTableMetadataBytes) {
throw new TachyonException("Too big table metadata: " + metadata.toString());
}
}
}
@@ -0,0 +1,79 @@
/*
* Licensed to the University of California, Berkeley under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You 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.
*/

package tachyon.master.next.rawtable.meta;

import java.nio.ByteBuffer;

import tachyon.util.io.BufferUtils;

public class RawTable {
/** Table ID */
private final int mId;
/** Number of columns */
private final int mColumns;
/** Table metadata */
private ByteBuffer mMetadata;

/**
* Create a new RawTable with metadata set to null. metadata can later be set via
* {@link #setMetadata(java.nio.ByteBuffer)}.
*
* @param id table id
* @param columns number of columns
*/
public RawTable(int id, int columns) {
mId = id;
mColumns = columns;
mMetadata = null;
}

/**
* Create a new RawTable with metadata set to a ByteBuffer.
*
* @param id table id
* @param columns number of columns
* @param metadata table metadata, if is null, the internal metadata is set to an empty buffer,
* otherwise, the provided buffer will be copied into the internal buffer.
* @return the created RawTable
*/
public RawTable(int id, int columns, ByteBuffer metadata) {
mId = id;
mColumns = columns;
setMetadata(metadata);
}

public int getId() {
return mId;
}

public int getColumns() {
return mColumns;
}

public ByteBuffer getMetadata() {
return mMetadata;
}

/**
* Set the table metadata. If the specified metadata is null, the internal metadata will be set to
* an empty byte buffer, otherwise, the provided metadata will be copied into the internal buffer.
*
* @param metadata the metadata to be set
*/
public void setMetadata(ByteBuffer metadata) {
mMetadata = BufferUtils.cloneByteBuffer(metadata);
}
}
126 changes: 126 additions & 0 deletions servers/src/main/java/tachyon/master/next/rawtable/meta/RawTables.java
@@ -0,0 +1,126 @@
/*
* Licensed to the University of California, Berkeley under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You 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.
*/

package tachyon.master.next.rawtable.meta;

import java.nio.ByteBuffer;

import tachyon.master.next.IndexedSet;
import tachyon.thrift.TableDoesNotExistException;
import tachyon.thrift.TachyonException;
import tachyon.util.io.BufferUtils;

/**
* All RawTables managed by RawTableMaster.
*/
public class RawTables {
private final IndexedSet.FieldIndex<RawTable> mIdIndex = new IndexedSet.FieldIndex<RawTable>() {
@Override
public Object getFieldValue(RawTable o) {
return o.getId();
}
};
/** A set of TableInfo indexed by table id */
private final IndexedSet<RawTable> mTables = new IndexedSet<RawTable>(mIdIndex);

/**
* Add a raw table.
*
* @param tableId The id of the raw table
* @param columns The number of columns in the raw table
* @param metadata The additional metadata of the raw table
* @return true if the raw table does not exist before and successfully added, false otherwise
* @throws TachyonException when metadata size is larger than maximum configured size
*/
public synchronized boolean add(int tableId, int columns, ByteBuffer metadata)
throws TachyonException {
if (mTables.contains(mIdIndex, tableId)) {
return false;
}
mTables.add(new RawTable(tableId, columns, metadata));
return true;
}

/**
* Remove a raw table.
*
* @param tableId The id of the raw table
* @return true if the table exists and successfully removed, false otherwise
*/
public synchronized boolean remove(int tableId) {
return mTables.removeByField(mIdIndex, tableId);
}

/**
* Whether the raw table exists.
*
* @param tableId the id of the raw table
* @return true if the table exists, false otherwise.
*/
public synchronized boolean contains(int tableId) {
return mTables.contains(mIdIndex, tableId);
}

/**
* Get the number of the columns of a raw table.
*
* @param tableId the id of the raw table
* @return the number of the columns, -1 if the table does not exist.
*/
public synchronized int getColumns(int tableId) {
RawTable table = mTables.getFirstByField(mIdIndex, tableId);
return null == table ? -1 : table.getColumns();
}

/**
* Get the metadata of the specified raw table.
*
* @param tableId The id of the raw table
* @return null if it has no metadata, or a copy of the internal metadata
*/
public synchronized ByteBuffer getMetadata(int tableId) {
RawTable table = mTables.getFirstByField(mIdIndex, tableId);
return null == table ? null : BufferUtils.cloneByteBuffer(table.getMetadata());
}

/**
* Get the raw table info.
*
* @param tableId the raw table id.
* @return the raw table info if the table exists, null otherwise.
*/
public synchronized RawTable getTableInfo(int tableId) {
return mTables.getFirstByField(mIdIndex, tableId);
}

/**
* Update the metadata of the specified raw table. If the new specified metadata is null, the
* internal metadata will be updated to an empty byte buffer.
*
* @param tableId The id of the raw table
* @param metadata The new metadata of the raw table
* @throws TableDoesNotExistException when the table does not exist
*/
public synchronized void updateMetadata(int tableId, ByteBuffer metadata)
throws TableDoesNotExistException {
RawTable table = mTables.getFirstByField(mIdIndex, tableId);

if (null == table) {
throw new TableDoesNotExistException("The raw table " + tableId + " does not exist.");
}

table.setMetadata(metadata);
}
}

0 comments on commit 9551cde

Please sign in to comment.