BurningBright

  • Home

  • Tags

  • Categories

  • Archives

  • Search

Mathjax in hexo

Posted on 2019-05-29 | Edited on 2020-07-09 | In blog

When $a \ne 0$, there are two solutions to \(ax^2 + bx + c = 0\) and they are $$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$

Simple inline $a = b + c$.
Block:

add ejs

themes/xxx/layout/_partial/mathjax.ejs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!-- MathJax配置,可通过单美元符号书写行内公式等 -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
"HTML-CSS": {
preferredFont: "TeX",
availableFonts: ["STIX","TeX"],
linebreaks: { automatic:true },
EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50)
},
tex2jax: {
inlineMath: [ ["$", "$"], ["\\(","\\)"] ],
processEscapes: true,
ignoreClass: "tex2jax_ignore|dno",
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
},
TeX: {
equationNumbers: { autoNumber: "AMS" },
noUndefined: { attributes: { mathcolor: "red", mathbackground: "#FFEEEE", mathsize: "90%" } },
Macros: { href: "{}" }
},
messageStyle: "none"
});
</script>
<!-- 给MathJax元素添加has-jax class -->
<script type="text/x-mathjax-config">
MathJax.Hub.Queue(function() {
var all = MathJax.Hub.getAllJax(), i;
for(i=0; i < all.length; i += 1) {
all[i].SourceElement().parentNode.className += ' has-jax';
}
});
</script>
<!-- 通过连接CDN加载MathJax的js代码 -->
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML">
</script>

add judge

themes/free2mind/layout/_partial/footer.ejs

1
2
3
4
<!-- 根据页面mathjax变量决定是否加载MathJax数学公式js -->
<% if (page.mathjax){ %>
<%- partial('mathjax') %>
<% } %>

Use NexT

clone https://github.com/theme-next/hexo-theme-next

1
2
npm uninstall hexo-renderer-marked --save
npm install hexo-renderer-kramed --save

modify node_modules\kramed\lib\rules\inline.js
line: 11

1
2
//escape: /^\\([\\`*{}\[\]()#$+\-.!_>])/,
escape: /^\\([`*\[\]()#$+\-.!_>])/,

line: 20

1
2
//em: /^\b_((?:__|[\s\S])+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,
em: /^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,

open switch in theme config
switch theme in blog config

done

https://docs.mathjax.org/en/latest/start.html
http://www.mamicode.com/info-detail-2145077.html
Hexo 的 Next 主题中渲染 MathJax 数学公式
Nunjucks Error expected variable end解决办法

Throttling pattern

Posted on 2019-05-20 | In java

throttling-pattern.png

Interface Throttler

Throttler defined a simple method used to call throttler implementaton.

public void start()

Implementaton

ThrottleTimerImpl implement Throttler used to cyclical reset service visit counter.

1
2
3
4
5
6
new Timer(true).schedule(new TimerTask() {
@Override
public void run() {
callsCount.reset();
}
}, 0, throttlePeriod);

Counter

CallsCount use ConcurrentHashMap to implement multi thread save counter.

private Map<String, AtomicLong> tenantCallsCount = new ConcurrentHashMap<>();

POJO

Tenant represent a kind of business.
have properties name and allowedCallsPerSecond

Service

XXXService call Throttler to start reset thread.
XXXService call counter to check whether ths visit under threshold.

1
2
3
4
5
6
7
String tenantName = tenant.getName();
long count = callsCount.getCount(tenantName);
if (count >= tenant.getAllowedCallsPerSecond()) {
// TODO throw excepton or return error code
return ;
}
callsCount.incrementCount(tenantName);

Summary

Throttling is used to limit client visit frequency, so we need:
a overall situation counter
a daemonize thread resetor
a business represent pojo
a limited flow service

bayes' formula comprehend

Posted on 2019-05-10 | Edited on 2020-06-16 | In math

Ideology

Old day people study probability forcus on sample distribute. [fix parameters]
Bayes probability study forcus on parameters.[fix samples]

1
prior distribution + sample info -> Posterior distribution

the new experiment result will refresh people cognition on old things.
It’s fit people cognition law on nation explor method.

formula

First:

the probability event A, event B happen at the same time
, equals the probability A happen multiply with the probability
B happen when A happen.

So we have:

=>

=>

a new knowledge = a priori knowledge * a new factor affect knowledge / a new factor

We use sample experiment’s phenomenon verify rules.
Use adjust factor to adust parameters, make rules more accurate.

Example

We need to check a rule:
watermelon sugar content > 10, good probability is 70%

  1. prior condition is good watermelon probability is 20%
  2. exp:bad watermelon has probability 25%, sugar content > 10
  3. exp:good watermelon has probability 99%, sugar content > 10

so we have :


Now we found the threshold is not right, base on adjust factor, we change rule:
watermelon sugar content > 15, good probability is 70%

  1. prior condition is good watermelon probability is 20%
  2. exp:bad watermelon has probability 15%, sugar content > 15
  3. exp:good watermelon has probability 90%, sugar content > 15

so we have :


Keep on adjust:
watermelon sugar content > 20, good probability is 70%

  1. prior condition is good watermelon probability is 20%
  2. exp:bad watermelon has probability 9%, sugar content > 20
  3. exp:good watermelon has probability 85%, sugar content > 20

https://www.cnblogs.com/zhoulujun/p/8893393.html

hide csdn ad in chrome

Posted on 2019-05-07 | Edited on 2019-05-20 | In javascript

Create manifest.json

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"name": "CSDN Ad Blocker",
"version": "1.0",
"description": "An simple extension to hide the ads from csdn website",
"manifest_version": 2,
"icons": {
"128": "bb.ico"
},
"content_scripts": [{
"js": ["content.js"],
"matches": ["https://blog.csdn.net/*"]
}]
}

choose an icon you like like ‘bb.ico’
declare extension function, matches website …

Create content.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
let adSelectors = [
'iframe',
'aside .csdn-tracking-statistics.mb8.box-shadow',
'aside .right_box.footer_box.csdn-tracking-statistics',
'.indexSuperise',
'#adContent',
'#reportContent',
'.recommend-box',
'.recommend-right',
'.pulllog-box',
'#article_content a img'
]

document.querySelectorAll(adSelectors.join(',')).forEach(item => {
item.style.display = 'none'
})

var readmore = document.getElementById("btn-readmore");
readmore.removeAttribute('data-track-click');
readmore.click();
1
2
3
4
5
6
7
8
9
10
11
# ad frame
# left up one
# left down recruit
# right superise
# right vip
# right report
# recommend box
# recommend right
# botton button bar
# login register bar
# article content ad image

hide target ad dom.
if you like, you can adjust the main DOM.

1
document.getElementsByTagName('main')[0].style.width = '75%'

this line in repository cause aside squeezed underneath the main dom
and i don’t like it, so i delete this line.

Install extension

in chrome open this url chrome://extensions/
open toggle developer mode
clike LOAD UNPACKED, choose project folder.

enjoy :-)

https://github.com/JerryYuanJ/demos/tree/master/02-simple-csdn-ad-blocker
https://blog.csdn.net/qq_25324335/article/details/82595836

compile spring 5.x in idea

Posted on 2019-04-29 | In java

prepare

  1. source code spring5.x
  2. gradle 4.9
  3. jdk1.8_131
  4. ideaIC-2019.1.1.exe (IntelliJ IDEA Community Edition 2019.1.1 x64)

Download source code in:
https://github.com/spring-projects/spring-framework/tree/5.0.x
Download gradle in:
https://gradle.org/releases/
Download idea in:
https://www.jetbrains.com/idea/download/
Download jdk in oracle website

the versions of these things are important

  • gradle version should bigger than 4.2 and no big than 5.0
    i use 4.6/ 5.1/ 5.4 gradle compile project failed.
  • jdk version should in 1.8, and no bigger than 1.8_131
    the blog of csdn editor has such experience.
  • idea version the newer the better, i use idea 2017.1.2 failed in junit test.

install

JAVA_HOME: D:\jdk1.8.0_131
CLASSPATH: .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar;

GRADLE_HOME: D:\gradle-4.6
GRADLE_USER_HOME: D:.gradle

Add to path

at setting of idea, set sdk/gradle path.
set gradle parameters -XX:MaxPermSize=1024m -Xmx1024m -XX:MaxHeapSize=1024m

20190429143526.png

compile

import source code of spring5.x, import as a gradle project.
wait for gradle solve project sync.

comment no use depend in spring-beans.gradle

1
//compileGroovy.dependsOn = compileGroovy.taskDependencies.values - "compileJava"

if meet dependency problem like this

1
2
3
Circular dependency between the following tasks:
...
...

add org.gradle.configureondemand = true in gradle.properties

test

project is ready :)

org.springframework.beans.factory.xml.BeanNameGenerationTests

https://blog.csdn.net/baomw/article/details/83956300
https://www.jianshu.com/p/d6e3ec44f99a

Bayes' theorem

Posted on 2019-04-11 | Edited on 2020-06-16 | In math

conditional probability

A/B are event, when B happen, A‘s conditional probability. ps.P(B) > 0

NB_3.png

seven ball in p1 p2
two white ball in p1
one white ball in p2
What’s the probability when fetch white from B bucket.

Event A is fetch white
Event B is fetch from P2 bucket

Multiplication formula

  1. formula

  2. when n >= 2 :

  3. full probability formula
    if event array B1, B2, B3, … satisfying

  • i ≠ j
  • i,j=1 2 ….

then array event is a divide of sample space Ω .

this formula mean no interference event array can calculate target sample space in divide.
It’s called prior probability

For example: spot check a set of different products’ good rate

bayes formula

bayes formula is the reverse process of full probability formula.
It research a happened event cause factor, not forcast it.
It’s called posterior probability

For example:
find killer in criminal suspect
spam filtering in e-mail system

https://www.cnblogs.com/ohshit/p/5629581.html

thread pool anaylsis

Posted on 2019-03-29 | Edited on 2019-04-29 | In java

queue1.gif
queue2.gif
queue3.jpg

properties

private final BlockingQueue workQueue;

private final ReentrantLock mainLock = new ReentrantLock();

private volatile long keepAliveTime;

private volatile boolean allowCoreThreadTimeOut;

private volatile int corePoolSize;

private volatile int maximumPoolSize;

private volatile int poolSize;

execute judgement

1
2
3
4
5
6
7
8
9
10
11
public void execute(Runnable command) {
if (command == null) throw new NullPointerException();
if (poolSize >= corePoolSize || !addIfUnderCorePoolSize(command)) {
if (runState == RUNNING && workQueue.offer(command)) {
if (runState != RUNNING || poolSize == 0)
ensureQueuedTaskHandled(command);
}
else if (!addIfUnderMaximumPoolSize(command))
reject(command); // is shutdown or saturated
}
}

thread judgement

1
2
3
4
5
6
7
8
9
10
11
12
private boolean addIfUnderCorePoolSize(Runnable firstTask) {
Thread t = null;
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
if (poolSize < corePoolSize && runState == RUNNING)
t = addThread(firstTask);
} finally {
mainLock.unlock();
}
return t != null;
}

add thread

1
2
3
4
5
6
7
8
9
10
11
12
13
private Thread addThread(Runnable firstTask) {

// similar to new Thread(Runnable);
Worker w = new Worker(firstTask);
Thread t = threadFactory.newThread(w);
...
int nt = ++poolSize;
if (nt > largestPoolSize) largestPoolSize = nt;
...
t.start();
...
return t;
}

Inner class Worker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private final class Worker implements Runnable {
...
private void runTask(Runnable task) {
...
task.run();
...
}
public void run() {
...
while (task != null || (task = getTask()) != null) {
runTask(task);
task = null;
}
...
workerDone(this);
}
}

get task from queue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Runnable getTask() {
for (;;) {
...
Runnable r;
if (state == SHUTDOWN) // Help drain queue
r = workQueue.poll();
else if (poolSize > corePoolSize || allowCoreThreadTimeOut)
r = workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS);
else
r = workQueue.take();
if (r != null)
return r;
...
}
}

the design of thread pool

Has no specific thread to dispatch task.

If still have task in QlockingQueue,
when thread’s task done, it will fetch task in queue itself.

If still no task in QlockingQueue, and task has done.
In worker’s run() - workerDone(this); call to terminate itself.

https://www.cnblogs.com/heyanan/p/9261695.html
http://www.cnblogs.com/dolphin0520/p/3932921.html

troubleshooting high cpu occupancy rate

Posted on 2019-03-27 | Edited on 2019-04-29 | In java

20190327092247.png

find target thread

top find abnormal rate cpu usage process
top -p [pid] -H find this process’s abnormal thread
printf "%x\n" tid change thread number to hexadecimal
jstack [pid] > xxx.log dump thread information
vim xxx.log find target hex id thread in file

log analysis

The state in dump file

  1. Deadlock
  2. Runnable
  3. Waiting on condition
  4. Waiting on monitor entry
  5. Suspended
  6. Object.wait() or TIMED_WAITING
  7. Blocked
  8. Parked

Waiting to lock and Blocked

1
2
3
4
5
6
7
kafka-coordinator-heartbeat-thread | xxxxx" #106 daemon prio=5 os_prio=0 tid=0x00007fee64001800 nid=0x214 waiting for monitor entry [0x00007fee1b6f5000]
java.lang.Thread.State: BLOCKED (on object monitor)
at org.apache.kafka.clients.consumer.internals.ConsumerNetworkClient.disableWakeups(ConsumerNetworkClient.java:409)
- waiting to lock <0x00000000c2a1b340> (a org.apache.kafka.clients.consumer.internals.ConsumerNetworkClient)
at org.apache.kafka.clients.consumer.internals.ConsumerNetworkClient.pollNoWakeup(ConsumerNetworkClient.java:264)
at org.apache.kafka.clients.consumer.internals.AbstractCoordinator$HeartbeatThread.run(AbstractCoordinator.java:865)
- locked <0x00000000c29fa1a8> (a org.apache.kafka.clients.consumer.internals.ConsumerCoordinator)

Waiting for monitor entry and in Object.wait()

1
2
3
4
5
6
7
8
9
10
11
12
pool-1-thread-2" #18 prio=5 os_prio=0 tid=0x00007fee5da7c000 nid=0x7f85 waiting on condition [0x00007fee9cdd6000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000c0b6db48> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

in Obejct.wait() and TIMED_WAITING

1
2
3
4
5
kafka-coordinator-heartbeat-thread | xxxxx" #89 daemon prio=5 os_prio=0 tid=0x00007fee2c007000 nid=0x202 in Object.wait() [0x00007fee289c8000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at org.apache.kafka.clients.consumer.internals.AbstractCoordinator$HeartbeatThread.run(AbstractCoordinator.java:884)
- locked <0x00000000c2cac000> (a org.apache.kafka.clients.consumer.internals.ConsumerCoordinator)

https://www.cnblogs.com/heyanan/p/9261695.html
https://www.cnblogs.com/paul8339/p/7464206.html

Lucene fmn index & output type

Posted on 2019-02-22 | Edited on 2019-03-09 | In search

Start fmn index

IndexWriter:addDocument -> DocumentWriter:addDocument

Document object in lucene store origin target’s properties
The property fieldInfos in DocumentWriter store fields

1
2
3
4
5
private FieldInfos fieldInfos;

fieldInfos = new FieldInfos();
fieldInfos.add(doc);
fieldInfos.write(directory, segment + ".fnm");

Build fmn index

1
2
3
4
5
6
7
8
9
10
11
12

public void write(IndexOutput output) throws IOException {
output.writeVInt(size());
for (int i = 0; i < size(); i++) {
FieldInfo fi = fieldInfo(i);
byte bits = 0x0;
// option bit setting
...
output.writeString(fi.name);
output.writeByte(bits);
}
}

write field size before loop
write field’s name in loop
write field’s setting in loop

If a document field’s name is name store in index and tokenized
then xxx.fmn will be like this

1
01 04 6e 61 6d 65 01

01 is fields size
04 is field length
6e 61 6d 65 is ASCII of name
01 is field setting

[size][01 lenght][01][01 setting][02 lenght][02][02 setting]……

Lucene type

In abstract class IndexOutput self define type method

1
2
3
4
5
6
7
8
9
10
public void writeVInt(int i) throws IOException {
while ((i & ~0x7F) != 0) {
writeByte((byte)((i & 0x7f) | 0x80));
i >>>= 7;
}
writeByte((byte)i);
}
public void writeVLong(long i) throws IOException {
...
}

The variable int/long format, smaller values take fewer bytes
int writes between one and five bytes.
long writes between one and nine bytes.

0x7F filt origin type no longer than 1 byte.
(i & 0x7F) | 0x80 make byte first bit is 1.


1
2
3
4
5
public void writeString(String s) throws IOException {
int length = s.length();
writeVInt(length);
writeChars(s, 0, length);
}

Write string’s length first.
Write String’s char array then.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void writeChars(String s, int start, int length) throws IOException {
final int end = start + length;
for (int i = start; i < end; i++) {
final int code = (int) s.charAt(i);
if (code >= 0x01 && code <= 0x7F)
writeByte((byte) code);
else if (((code >= 0x80) && (code <= 0x7FF)) || code == 0) {
writeByte((byte) (0xC0 | (code >> 6)));
writeByte((byte) (0x80 | (code & 0x3F)));
} else {
writeByte((byte) (0xE0 | (code >>> 12)));
writeByte((byte) (0x80 | ((code >> 6) & 0x3F)));
writeByte((byte) (0x80 | (code & 0x3F)));
}
}
}

Write ASCII directly [1 byte]
Write UTF-8 two bytes [2 byte]
Write UTF-8 three bytes [3 byte]

download outside GFW

Posted on 2019-02-21 | Edited on 2019-03-09 | In python

Download file in outsea server

https://www.katacoda.com/
https://www.katacoda.com/courses/ubuntu/playground

1
curl -O https://iterm2.com/downloads/stable/iTerm2-3_2_7.zip

Make channel through local NAT or firewall

https://ngrok.com/
Expose local machine to internet

1
ngrok http 80

Run a upload service

run a local server by python

1
pip install bottle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
from bottle import *

base_path = os.path.dirname(os.path.realpath(__file__)) # get script path

upload_path = os.path.join(base_path, 'upload') # upload file directory
if not os.path.exists(upload_path):
os.makedirs(upload_path)

@route('/upload', method='POST')
def do_upload():
"""processing uploaded file"""
filedata = request.files.get('fileField')

if filedata.file:
file_name = os.path.join(upload_path, filedata.filename)
try:
filedata.save(file_name) # write file
except IOError:
return 'upload failed'
return 'success, file name: {}'.format(file_name)
else:
return 'upload failed'

run(port=80, reloader=False) # reloader=True auto-reloading code

Upload file from remote to local

1
2
man curl
curl -F fileField=@iTerm2-3_2_7.zip https://83bc4ec8.ngrok.io/upload -v

PS

use ngrok tcp mode may much simpler
install ngrok client in remote machine may channel through

https://bbs.csdn.net/topics/392190705
https://blog.csdn.net/u011884440/article/details/70504653?utm_source=blogxgwz6
https://blog.csdn.net/fungleo/article/details/80703365

1…101112…29

Leon

282 posts
20 categories
58 tags
GitHub
Links
  • clock
  • typing-cn
  • mathjax
  • katex
  • cron
  • dos
  • keyboard
  • regex
  • sql
  • toy
© 2017 – 2024 Leon
Powered by Hexo v3.9.0
|
Theme – NexT.Muse v7.1.2