Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #49 and Issue #50 : Offheap Resource #51

Merged
merged 4 commits into from
Mar 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions offheap-resource/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.terracotta</groupId>
<artifactId>platform-root</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>offheap-resource</artifactId>

<dependencies>
<dependency>
<!-- This is dep is probably wrong -->
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.18</version>
</dependency>
<dependency>
<groupId>org.terracotta.internal</groupId>
<artifactId>tc-config-parser</artifactId>
<version>10.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchains>
<jdk>
<version>1.8</version>
</jdk>
</toolchains>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.1</version>
<configuration>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>org.terracotta.offheapresource.config</packageName>
<sources>
<source>src/main/resources/offheap-resource.xsd</source>
</sources>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* The contents of this file are subject to the Terracotta Public 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://terracotta.org/legal/terracotta-public-license.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Covered Software is OffHeap Resource.
*
* The Initial Developer of the Covered Software is
* Terracotta, Inc., a Software AG company
*/

package org.terracotta.offheapresource;

import java.util.concurrent.atomic.AtomicLong;

/**
* Represents an offheap resource, providing a reservation system that can be
* used to control the combined memory usage of participating consumers.
* <p>
* Reservation and release calls perform no allocations, and therefore rely on
* the cooperation of callers to achieve control over the 'real' resource usage.
*/
public class OffHeapResource {

private final AtomicLong remaining;

/**
* Creates a resource of the given initial size.
*
* @param size size of the resource
* @throws IllegalArgumentException if the size is negative
*/
OffHeapResource(long size) throws IllegalArgumentException {
if (size < 0) {
throw new IllegalArgumentException("Resource size cannot be negative");
} else {
this.remaining = new AtomicLong(size);
}
}

/**
* Reserves the given amount of this resource.
* <p>
* This method <em>performs no allocation</em>. It is simply a reservation
* that the consumer agrees to bind by the result of. A {@code false} return
* should mean the caller refrains from performing any associated allocation.
*
* @param size reservation size
* @return {@code true} if the reservation succeeded
* @throws IllegalArgumentException if the reservation size is negative
*/
public boolean reserve(long size) throws IllegalArgumentException {
if (size < 0) {
throw new IllegalArgumentException("Reservation size cannot be negative");
} else {
for (long current = remaining.get(); current >= size; current = remaining.get()) {
if (remaining.compareAndSet(current, current - size)) {
return true;
}
}
return false;
}
}

/**
* Releases the given amount of resource back to this pool.
*
* @param size release size
* @throws IllegalArgumentException if the release size is negative
*/
public void release(long size) throws IllegalArgumentException {
if (size < 0) {
throw new IllegalArgumentException("Released size cannot be negative");
} else {
remaining.addAndGet(size);
}
}

/**
* Returns the size of the remaining resource that can be reserved.
*
* @return the remaining resource size
*/
public long available() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't that one be called remaining?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The distinction being?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like remaining better. :)

return remaining.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* The contents of this file are subject to the Terracotta Public 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://terracotta.org/legal/terracotta-public-license.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Covered Software is OffHeap Resource.
*
* The Initial Developer of the Covered Software is
* Terracotta, Inc., a Software AG company
*/

package org.terracotta.offheapresource;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.SchemaFactory;
import org.terracotta.config.service.ServiceConfigParser;
import org.terracotta.entity.ServiceProviderConfiguration;
import org.w3c.dom.Element;

import org.terracotta.offheapresource.config.OffheapResourcesType;
import org.xml.sax.SAXException;

public class OffHeapResourceConfigurationParser implements ServiceConfigParser {

private static final URL XML_SCHEMA = OffHeapResourceConfigurationParser.class.getResource("/offheap-resource.xsd");
private static final URI NAMESPACE = URI.create("http://www.terracotta.org/config/offheap-resource");

@Override
public Source getXmlSchema() throws IOException {
return new StreamSource(XML_SCHEMA.openStream());
}

@Override
public URI getNamespace() {
return NAMESPACE;
}

@Override
public ServiceProviderConfiguration parse(Element elmnt, String string) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(OffheapResourcesType.class.getPackage().getName(), OffHeapResourceConfigurationParser.class.getClassLoader());
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setSchema(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(getXmlSchema()));
JAXBElement<OffheapResourcesType> parsed = (JAXBElement<OffheapResourcesType>) unmarshaller.unmarshal(elmnt);
return new OffHeapResourcesConfiguration(parsed.getValue());
} catch (JAXBException e) {
throw new IllegalArgumentException(e);
} catch (SAXException e) {
throw new AssertionError(e);
} catch (IOException e) {
throw new AssertionError(e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* The contents of this file are subject to the Terracotta Public 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://terracotta.org/legal/terracotta-public-license.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Covered Software is OffHeap Resource.
*
* The Initial Developer of the Covered Software is
* Terracotta, Inc., a Software AG company
*/

package org.terracotta.offheapresource;

import org.terracotta.entity.ServiceConfiguration;

/**
*
* @author cdennis
*/
public final class OffHeapResourceIdentifier implements ServiceConfiguration<OffHeapResource> {

private final String name;

public static OffHeapResourceIdentifier identifier(String name) {
return new OffHeapResourceIdentifier(name);
}

private OffHeapResourceIdentifier(String name) {
if (name == null) {
throw new NullPointerException("Name cannot be null");
} else {
this.name = name;
}
}

@Override
public int hashCode() {
return name.hashCode();
}

@Override
public boolean equals(Object obj) {
return (obj instanceof OffHeapResourceIdentifier) && name.equals(((OffHeapResourceIdentifier) obj).name);
}

@Override
public Class<OffHeapResource> getServiceType() {
return OffHeapResource.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* The contents of this file are subject to the Terracotta Public 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://terracotta.org/legal/terracotta-public-license.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Covered Software is OffHeap Resource.
*
* The Initial Developer of the Covered Software is
* Terracotta, Inc., a Software AG company
*/

package org.terracotta.offheapresource;

import java.util.Collection;
import org.terracotta.entity.ServiceProvider;
import org.terracotta.entity.ServiceProviderConfiguration;
import org.terracotta.offheapresource.config.OffheapResourcesType;
import org.terracotta.offheapresource.config.ResourceType;

class OffHeapResourcesConfiguration implements ServiceProviderConfiguration {

private final OffheapResourcesType xmlConfig;

OffHeapResourcesConfiguration(OffheapResourcesType xmlConfig) {
this.xmlConfig = xmlConfig;
}

public Collection<ResourceType> getResources() {
return xmlConfig.getResource();
}

@Override
public Class<? extends ServiceProvider> getServiceProviderType() {
return OffHeapResourcesProvider.class;
}

}
Loading