Skip to content

Commit

Permalink
Using cursor causes OOM mybatis#2812
Browse files Browse the repository at this point in the history
  • Loading branch information
gallyamb committed Feb 20, 2023
1 parent f35083d commit d5d83ab
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 0 deletions.
@@ -0,0 +1,114 @@
/*
* Copyright 2009-2023 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
*
* https://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.apache.ibatis.submitted.cursor_cache_oom;

import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.cursor.defaults.DefaultCursor;
import org.apache.ibatis.executor.resultset.DefaultResultSetHandler;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.defaults.DefaultSqlSession;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

@SuppressWarnings("ALL")
class CursorOomTest {

private static SqlSessionFactory sqlSessionFactory;

@BeforeAll
static void setUp() throws Exception {
// create an SqlSessionFactory
try (Reader reader = Resources.getResourceAsReader(
"org/apache/ibatis/submitted/cursor_cache_oom/mybatis-config.xml")) {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}

// populate in-memory database
BaseDataTest.runScript(
sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
"org/apache/ibatis/submitted/cursor_cache_oom/CreateDB.sql"
);
}

private static Map<CacheKey, Object> getNestedResultObjects(Cursor<User> users) throws IllegalAccessException,
NoSuchFieldException {
DefaultCursor<User> defaultCursor = (DefaultCursor<User>) users;
Field resultSetHandlerField = DefaultCursor.class.getDeclaredField("resultSetHandler");
resultSetHandlerField.setAccessible(true);
DefaultResultSetHandler defaultResultSetHandler =
(DefaultResultSetHandler) resultSetHandlerField
.get(defaultCursor);
Field nestedResultObjectsField = DefaultResultSetHandler.class.getDeclaredField("nestedResultObjects");
nestedResultObjectsField.setAccessible(true);
return (Map<CacheKey, Object>) nestedResultObjectsField
.get(defaultResultSetHandler);
}

private static List<Cursor<?>> getCursors(SqlSession sqlSession)
throws NoSuchFieldException, IllegalAccessException {
DefaultSqlSession session = (DefaultSqlSession) sqlSession;
Field cursorListField = DefaultSqlSession.class.getDeclaredField("cursorList");
cursorListField.setAccessible(true);
List<Cursor<?>> cursorList =
(List<Cursor<?>>) cursorListField.get(session);
return cursorList;
}

@Test
void shouldNotCacheAllDataForWholeSessionWhileUsingCursor() throws IOException, NoSuchFieldException,
IllegalAccessException {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
try (Cursor<User> users = mapper.fetchUsers()) {
for (User user : users) {
consumeUser(user);
}
Map nestedResultObjects = getNestedResultObjects(users);

Assertions.assertFalse(nestedResultObjects.isEmpty());

// does not pass now
// will be great, if cursor will use constant memory instead of linear one
// Assertions.assertTrue(nestedResultObjects.size() <= 2);
}

List<Cursor<?>> cursorList = getCursors(sqlSession);

// expect that either reference to the cursor itselfis gone or cursor does not contains all the fetched data
// the most preferrable way will be not to cache data, when the row is already processed (see commented
// line above)
Assertions.assertTrue(
cursorList.isEmpty() || getNestedResultObjects((Cursor<User>) cursorList.get(0)).isEmpty()
);
}
}

private void consumeUser(User user) {
// do nothing
}
}
@@ -0,0 +1,29 @@
/*
* Copyright 2009-2022 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
*
* https://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.apache.ibatis.submitted.cursor_cache_oom;

public class Friend {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
@@ -0,0 +1,27 @@
/*
* Copyright 2009-2022 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
*
* https://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.apache.ibatis.submitted.cursor_cache_oom;

import org.apache.ibatis.cursor.Cursor;

public interface Mapper {

User getUser(Integer id);

void insertUser(User user);

Cursor<User> fetchUsers();
}
@@ -0,0 +1,47 @@
/*
* Copyright 2009-2022 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
*
* https://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.apache.ibatis.submitted.cursor_cache_oom;

public class User {

private Integer id;
private String name;
private Friend friend;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Friend getFriend() {
return friend;
}

public void setFriend(Friend friend) {
this.friend = friend;
}
}
@@ -0,0 +1,29 @@
--
-- Copyright 2009-2022 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
--
-- https://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.
--

drop table users if exists;

create table users (
id int,
name varchar(20),
friend_id int
);

create sequence IF NOT EXISTS cursor_cache_oom START WITH 1 INCREMENT BY 1;

insert into users (id, name, friend_id)
SELECT nr, 'test' + nr, mod((nr + 1), 10000) + 1
from unnest(sequence_array(1, 10000, 1)) as i(nr);
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2009-2022 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
https://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.
-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="org.apache.ibatis.submitted.cursor_cache_oom.Mapper">

<select id="getUser" resultType="org.apache.ibatis.submitted.cursor_cache_oom.User">
select * from users where id = #{id}
</select>

<insert id="insertUser">
insert into users values(#{id}, #{name})
</insert>

<select id="fetchUsers" resultMap="User">
select u.id as id, u.name as name, f.name as friend_name
from users as u
left join users as f on u.friend_id = f.id
</select>

<resultMap id="User" type="org.apache.ibatis.submitted.cursor_cache_oom.User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<association property="friend" resultMap="Friend"/>
</resultMap>

<resultMap id="Friend" type="org.apache.ibatis.submitted.cursor_cache_oom.Friend">
<result property="name" column="friend_name"/>
</resultMap>
</mapper>
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright 2009-2022 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
https://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.
-->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="" value="" />
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:basetest" />
<property name="username" value="sa" />
</dataSource>
</environment>
</environments>

<mappers>
<mapper class="org.apache.ibatis.submitted.cursor_cache_oom.Mapper" />
</mappers>

</configuration>

0 comments on commit d5d83ab

Please sign in to comment.