Polish DataSource metrics
commit
2262a3baa7
@ -1,67 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.jdbc;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* A {@link DataSourceMetadataProvider} implementation that returns the first
|
||||
* {@link DataSourceMetadata} that is found by one of its delegate.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public class CompositeDataSourceMetadataProvider implements DataSourceMetadataProvider {
|
||||
|
||||
private final Collection<DataSourceMetadataProvider> providers;
|
||||
|
||||
/**
|
||||
* Create an instance with an initial collection of delegates to use.
|
||||
*/
|
||||
public CompositeDataSourceMetadataProvider(Collection<DataSourceMetadataProvider> providers) {
|
||||
this.providers = providers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance with no delegate.
|
||||
*/
|
||||
public CompositeDataSourceMetadataProvider() {
|
||||
this(new ArrayList<DataSourceMetadataProvider>());
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSourceMetadata getDataSourceMetadata(DataSource dataSource) {
|
||||
for (DataSourceMetadataProvider provider : providers) {
|
||||
DataSourceMetadata dataSourceMetadata = provider.getDataSourceMetadata(dataSource);
|
||||
if (dataSourceMetadata != null) {
|
||||
return dataSourceMetadata;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a {@link DataSourceMetadataProvider} delegate to the list.
|
||||
*/
|
||||
public void addDataSourceMetadataProvider(DataSourceMetadataProvider provider) {
|
||||
this.providers.add(provider);
|
||||
}
|
||||
|
||||
}
|
@ -1,73 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.jdbc;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* Provide various metadata regarding a {@link DataSource} that
|
||||
* are shared by most data source types but not accessible in a
|
||||
* standard manner.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public interface DataSourceMetadata {
|
||||
|
||||
/**
|
||||
* Return the usage of the pool as a double value between
|
||||
* 0 and 1.
|
||||
* <ul>
|
||||
* <li>1 means that the maximum number of connections
|
||||
* have been allocated</li>
|
||||
* <li>0 means that no connection is currently active</li>
|
||||
* <li>-1 means there is not limit to the number of connections
|
||||
* that can be allocated</li>
|
||||
* </ul>
|
||||
* This may also return {@code null} if the data source does
|
||||
* not provide the necessary information to compute the poll usage.
|
||||
*/
|
||||
Float getPoolUsage();
|
||||
|
||||
/**
|
||||
* Return the current number of active connections that
|
||||
* have been allocated from the data source or {@code null}
|
||||
* if that information is not available.
|
||||
*/
|
||||
Integer getPoolSize();
|
||||
|
||||
/**
|
||||
* Return the maximum number of active connections that can be
|
||||
* allocated at the same time or {@code -1} if there is no
|
||||
* limit. Can also return {@code null} if that information is
|
||||
* not available.
|
||||
*/
|
||||
Integer getMaxPoolSize();
|
||||
|
||||
/**
|
||||
* Return the minimum number of idle connections in the pool
|
||||
* or {@code null} if that information is not available.
|
||||
*/
|
||||
Integer getMinPoolSize();
|
||||
|
||||
/**
|
||||
* Return the query to use to validate that a connection is
|
||||
* valid or {@code null} if that information is not available.
|
||||
*/
|
||||
String getValidationQuery();
|
||||
|
||||
}
|
@ -1,111 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.jdbc;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import com.zaxxer.hikari.pool.HikariPool;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
|
||||
/**
|
||||
* A {@link DataSourceMetadata} implementation for the hikari
|
||||
* data source.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public class HikariDataSourceMetadata extends AbstractDataSourceMetadata<HikariDataSource> {
|
||||
|
||||
|
||||
private final HikariPoolProvider hikariPoolProvider;
|
||||
|
||||
public HikariDataSourceMetadata(HikariDataSource dataSource) {
|
||||
super(dataSource);
|
||||
this.hikariPoolProvider = new HikariPoolProvider(dataSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getPoolSize() {
|
||||
HikariPool hikariPool = hikariPoolProvider.getHikariPool();
|
||||
if (hikariPool != null) {
|
||||
return hikariPool.getActiveConnections();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Integer getMaxPoolSize() {
|
||||
return getDataSource().getMaximumPoolSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getMinPoolSize() {
|
||||
return getDataSource().getMinimumIdle();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValidationQuery() {
|
||||
return getDataSource().getConnectionTestQuery();
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide the {@link HikariPool} instance managed internally by
|
||||
* the {@link HikariDataSource} as there is no other way to retrieve
|
||||
* that information except JMX access.
|
||||
*/
|
||||
private static class HikariPoolProvider {
|
||||
private final HikariDataSource dataSource;
|
||||
|
||||
private boolean poolAvailable;
|
||||
|
||||
private HikariPoolProvider(HikariDataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
this.poolAvailable = isHikariPoolAvailable();
|
||||
}
|
||||
|
||||
public HikariPool getHikariPool() {
|
||||
if (!poolAvailable) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Object value = doGetValue();
|
||||
if (value instanceof HikariPool) {
|
||||
return (HikariPool) value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean isHikariPoolAvailable() {
|
||||
try {
|
||||
doGetValue();
|
||||
return true;
|
||||
}
|
||||
catch (BeansException e) { // No such field
|
||||
return false;
|
||||
}
|
||||
catch (SecurityException e) { // Security manager prevents to read the value
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private Object doGetValue() {
|
||||
DirectFieldAccessor accessor = new DirectFieldAccessor(this.dataSource);
|
||||
return accessor.getPropertyValue("pool");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.jdbc.metadata;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* Provides access meta-data that is commonly available from most polled
|
||||
* {@link DataSource} implementations.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public interface DataSourcePoolMetadata {
|
||||
|
||||
/**
|
||||
* Return the usage of the pool as value between 0 and 1 (or -1 if the pool is not
|
||||
* limited).
|
||||
* <ul>
|
||||
* <li>1 means that the maximum number of connections have been allocated</li>
|
||||
* <li>0 means that no connection is currently active</li>
|
||||
* <li>-1 means there is not limit to the number of connections that can be allocated</li>
|
||||
* </ul>
|
||||
* This may also return {@code null} if the data source does not provide the necessary
|
||||
* information to compute the poll usage.
|
||||
*/
|
||||
Float getUsage();
|
||||
|
||||
/**
|
||||
* Return the current number of active connections that have been allocated from the
|
||||
* data source or {@code null} if that information is not available.
|
||||
*/
|
||||
Integer getActive();
|
||||
|
||||
/**
|
||||
* Return the maximum number of active connections that can be allocated at the same
|
||||
* time or {@code -1} if there is no limit. Can also return {@code null} if that
|
||||
* information is not available.
|
||||
*/
|
||||
Integer getMax();
|
||||
|
||||
/**
|
||||
* Return the minimum number of idle connections in the pool or {@code null} if that
|
||||
* information is not available.
|
||||
*/
|
||||
Integer getMin();
|
||||
|
||||
/**
|
||||
* Return the query to use to validate that a connection is valid or {@code null} if
|
||||
* that information is not available.
|
||||
*/
|
||||
String getValidationQuery();
|
||||
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.jdbc.metadata;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* A {@link DataSourcePoolMetadataProvider} implementation that returns the first
|
||||
* {@link DataSourcePoolMetadata} that is found by one of its delegate.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public class DataSourcePoolMetadataProviders implements DataSourcePoolMetadataProvider {
|
||||
|
||||
private final List<DataSourcePoolMetadataProvider> providers;
|
||||
|
||||
/**
|
||||
* Create a {@link DataSourcePoolMetadataProviders} instance with an initial
|
||||
* collection of delegates to use.
|
||||
*/
|
||||
public DataSourcePoolMetadataProviders(
|
||||
Collection<? extends DataSourcePoolMetadataProvider> providers) {
|
||||
this.providers = new ArrayList<DataSourcePoolMetadataProvider>(providers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSourcePoolMetadata getDataSourcePoolMetadata(DataSource dataSource) {
|
||||
for (DataSourcePoolMetadataProvider provider : this.providers) {
|
||||
DataSourcePoolMetadata metadata = provider
|
||||
.getDataSourcePoolMetadata(dataSource);
|
||||
if (metadata != null) {
|
||||
return metadata;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.jdbc.metadata;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import com.zaxxer.hikari.pool.HikariPool;
|
||||
|
||||
/**
|
||||
* {@link DataSourcePoolMetadata} for a Hikari {@link DataSource}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public class HikariDataSourcePoolMetadata extends
|
||||
AbstractDataSourcePoolMetadata<HikariDataSource> {
|
||||
|
||||
public HikariDataSourcePoolMetadata(HikariDataSource dataSource) {
|
||||
super(dataSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getActive() {
|
||||
try {
|
||||
return getHikariPool().getActiveConnections();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private HikariPool getHikariPool() {
|
||||
return (HikariPool) new DirectFieldAccessor(getDataSource())
|
||||
.getPropertyValue("pool");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getMax() {
|
||||
return getDataSource().getMaximumPoolSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getMin() {
|
||||
return getDataSource().getMinimumIdle();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValidationQuery() {
|
||||
return getDataSource().getConnectionTestQuery();
|
||||
}
|
||||
|
||||
}
|
@ -1,84 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.jdbc;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class CompositeDataSourceMetadataProviderTests {
|
||||
|
||||
@Mock
|
||||
private DataSourceMetadataProvider firstProvider;
|
||||
|
||||
@Mock
|
||||
private DataSourceMetadata first;
|
||||
|
||||
@Mock
|
||||
private DataSource firstDataSource;
|
||||
|
||||
@Mock
|
||||
private DataSourceMetadataProvider secondProvider;
|
||||
|
||||
@Mock
|
||||
private DataSourceMetadata second;
|
||||
|
||||
@Mock
|
||||
private DataSource secondDataSource;
|
||||
|
||||
@Mock
|
||||
private DataSource unknownDataSource;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
given(firstProvider.getDataSourceMetadata(firstDataSource)).willReturn(first);
|
||||
given(firstProvider.getDataSourceMetadata(secondDataSource)).willReturn(second);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createWithProviders() {
|
||||
CompositeDataSourceMetadataProvider provider =
|
||||
new CompositeDataSourceMetadataProvider(Arrays.asList(firstProvider, secondProvider));
|
||||
assertSame(first, provider.getDataSourceMetadata(firstDataSource));
|
||||
assertSame(second, provider.getDataSourceMetadata(secondDataSource));
|
||||
assertNull(provider.getDataSourceMetadata(unknownDataSource));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addProvider() {
|
||||
CompositeDataSourceMetadataProvider provider =
|
||||
new CompositeDataSourceMetadataProvider();
|
||||
assertNull(provider.getDataSourceMetadata(firstDataSource));
|
||||
provider.addDataSourceMetadataProvider(firstProvider);
|
||||
assertSame(first, provider.getDataSourceMetadata(firstDataSource));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.jdbc.metadata;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Tests for {@link DataSourcePoolMetadataProviders}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class DataSourcePoolMetadataProvidersTests {
|
||||
|
||||
@Mock
|
||||
private DataSourcePoolMetadataProvider firstProvider;
|
||||
|
||||
@Mock
|
||||
private DataSourcePoolMetadata first;
|
||||
|
||||
@Mock
|
||||
private DataSource firstDataSource;
|
||||
|
||||
@Mock
|
||||
private DataSourcePoolMetadataProvider secondProvider;
|
||||
|
||||
@Mock
|
||||
private DataSourcePoolMetadata second;
|
||||
|
||||
@Mock
|
||||
private DataSource secondDataSource;
|
||||
|
||||
@Mock
|
||||
private DataSource unknownDataSource;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
given(this.firstProvider.getDataSourcePoolMetadata(this.firstDataSource))
|
||||
.willReturn(this.first);
|
||||
given(this.firstProvider.getDataSourcePoolMetadata(this.secondDataSource))
|
||||
.willReturn(this.second);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createWithProviders() {
|
||||
DataSourcePoolMetadataProviders provider = new DataSourcePoolMetadataProviders(
|
||||
Arrays.asList(this.firstProvider, this.secondProvider));
|
||||
assertSame(this.first, provider.getDataSourcePoolMetadata(this.firstDataSource));
|
||||
assertSame(this.second, provider.getDataSourcePoolMetadata(this.secondDataSource));
|
||||
assertNull(provider.getDataSourcePoolMetadata(this.unknownDataSource));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue