Search code examples
chroniclechronicle-queue

Chronicle how to increase processors?


I am using Chronicle queue 5.16.8

I am getting this warning

net.openhft.chronicle.threads.Pauser     : Using Pauser.sleepy() as not enough processors, have 1, needs 8+

Is it possible to increase this processor ?

source code

My approach here is use a Chronicle Map for storing index reader. I guess, I can have the same behavior turn in on recordHistory ...

I had to use Jackson Json converted ... I didn't get how to use writeDocument method.

The While true loop, it another not nice thing that I have here ... I couldn't figure how to find last index in the queue.

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.ChronicleMapBuilder;
import net.openhft.chronicle.queue.ChronicleQueue;
import net.openhft.chronicle.queue.ExcerptAppender;
import net.openhft.chronicle.queue.ExcerptTailer;
import net.openhft.chronicle.queue.RollCycles;
import net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.LinkedList;
import java.util.List;



@Service
public class QueueService {

    public static final String INDEX = "index";

    private ObjectMapper mapper = new ObjectMapper(); // json converter

    @PostConstruct
    public void init() {
        mapper.registerModule(new JavaTimeModule());
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    }

    public void write(List dtos, String path) throws Exception {

        try (ChronicleQueue queue = SingleChronicleQueueBuilder.binary(path).rollCycle(RollCycles.DAILY).build()) {
            final ExcerptAppender appender = queue.acquireAppender();

            for (int i=0; i<dtos.size(); i++) {
                appender.writeText(mapper.writeValueAsString(dtos.get(i)));
            }
        }
    }

    public void write(Object dto, String path) throws Exception {

        try (ChronicleQueue queue = SingleChronicleQueueBuilder.binary(path).rollCycle(RollCycles.DAILY).build()) {
            final ExcerptAppender appender = queue.acquireAppender();
            appender.writeText(mapper.writeValueAsString(dto));
        }
    }


    public List readList(String path, Class aClass) throws Exception {

        List dtoList = new LinkedList<>();

        try (ChronicleQueue queue = SingleChronicleQueueBuilder.binary(path).build()) {

            final ExcerptTailer tailer = queue.createTailer();

            ChronicleMap<String, Long> indexMap = getReaderIndexMap(queue.fileAbsolutePath());

            if (indexMap.containsKey(INDEX)) {
                tailer.moveToIndex(indexMap.get(INDEX));
            }

            while (true) { // something smart ?

                String json = tailer.readText();
                if (json == null) {
                    break;
                } else {
                    dtoList.add(mapper.readValue(json, aClass));
                }
            }

            indexMap.put(INDEX, tailer.index());
            indexMap.close();
        }
        return dtoList;
    }

    public ChronicleMap<String, Long> getReaderIndexMap(String queueName) throws Exception {

        ChronicleMapBuilder<String, Long> indexReaderMap = ChronicleMapBuilder.of(String.class, Long.class)
                .name("index-reader-map")
                .averageKey(INDEX)
                .entries(1);

        ChronicleMap<String, Long> map = indexReaderMap.createPersistedTo(new File(queueName+"/reader.idx"));
        return map;
    }
}

Solution

  • This is based on the number of available processors Java believes you have.

    If you have a virtual machine, you can configure your host to have more CPUs.

    If you have a physical machine, you can change your processor to one with more cores.

    Or you can ignore the warning.

    Busy pausing with only one CPU is probably not a good idea as it will use all the CPU you have.

    NOTE: We generally recommend having at least 4 cores, even for development.