Author Archives: admin

说说《大圣归来》

其实本来我是不打算看这部电影的,刚好公司组织一起去看,原价35的3D票只要10块钱,算是个福利,似乎网上一片称赞,索性就看一看。我最近刚把 GTA 5 打通,一篇很长的评论还没写完,就先来说说这个《大圣归来》。

首先得感谢这电影票只花了10块钱,要是35买的看完我肯定觉得不值。接下来一样样的评。

画面:很好。这部电影在画面上的突破,相对于其它国产动画还是很明显的。不夸张的说,这样的画面素质,放到迪士尼的那一堆3D动画片里也是不算差的。毛发的细节,光照,物理效果,都很好。可惜的是,画面好也就止步于此了,这么好的画面没有用来营造气氛渲染情感,妖洞没有妖气,那个关大圣的水晶宫其实很漂亮可以多给点特写但是稍微摆弄了两下就过去了,没有物尽其用。

剧情:弱爆了。一个好的剧情,要么是讲一个大家都没听过的故事,要么是讲一个大家以为都听过实际上走向出乎意料的故事。前者是盗梦空间,后者是冰雪奇缘。大圣的故事,看到开头猜到结尾,结果导演连个结尾都不让我们看,连个完整的故事都不讲完故意吊着你,不想多说了。

人物:既然片名叫大圣归来,那么我想主角肯定是大圣吧,其它什么江流儿老师傅猪八戒应该都是配角才对吧?可是塑造出来了一个什么样的大圣形象呢?恕我眼拙看不出来,因为封印没解掉垂头丧气,因为看到江流儿被妖怪打而变身奥特曼,体现出大圣是一个爱护儿童的好猴子?可是论爱护儿童,我觉得师傅才是真·爱啊,人家没有特技都敢入妖穴,比大圣这种有特技的高到不知道哪里去了。

音乐:那个汪峰的歌一出来就彻底出戏了。现在抓妖精、救女孩的事情一个没做出这么燃的BGM是要怎样?好在其它的配乐分寸把握的都还好,至少不是负分。

总体而言,个人觉得这种某个方面有明显瑕疵的电影评分在7到8之间算是一个正常的评价。大圣归来,并没有好到值得让你专门抽两个小时的时间,认认真真欣赏的水平;如果大圣归来是迪士尼出的,我会毫不客气的把他划到流水线片、爆米花片的类别里去。

一个由于Nginx配置不当导致的启动失败: Stopping System V runlevel compatibility

最近在做Android Push系统的服务器端, 要用到1.9版Nginx引入的TCP代理功能, 由于Nginx默认的连接数太少, 我就按照之前改内核参数的习惯, 直接大手一挥直接把连接数加到了1000W. reload配置之后, 我的机器死掉了.

我当时根本没想到是Nginx的原因, 下意识的认为是我用的那个MQTT的库一定是泄露内存了, 然后果断重启机器.

然后就起不来了, 在Ubuntu的启动界面一直转啊转. 再次重启, 进 Recovery Mode 打日志, 发现卡在 Stopping System V runlevel compatibility [OK] 这里.

网上几乎一边倒的认为是 NVidia 的显卡驱动问题, 虽然我觉得不太可能是这个原因不过网上都这么说, 那就卸了吧.

卸了之后还是进不去系统, 而且还是卡在老地方! 于是开始在笔记本上查资料, 机器就放在那里没管. 过了几分钟之后, 我瞟了一眼机器, 出了一行 log, Out of Memory Error, Kill Nginx. 这时候我才意识到到是不是和我之前改了 Nginx 配置有关系. 很快我就确定是 Nginx 的问题, 因为 Nginx 用的是连接池, 即使没有连接也会预先创建好一定数量的备用, 我的机器大约8G内存, 之前的测试中大约能抗住80W左右的连接, 1000W的连接池必定导致OOM, 然后Nginx就吃光了所有的内存, 强迫操作系统不断进行垃圾回收, 导致启动卡死.

接下来就很简单了, 把Nginx的连接数改回去, 再把显卡驱动装上, 成功进入系统, 然后再给Nginx设置一个合适的连接池大小, 继续进行试验.

Closures in different languages

In most scripting languages, there are first-class functions. In short, first-class functions refer to functions that can serve as call arguments, work in expressions and be assigned to variables.

So what is a closure? A closure is a function that brings context information with it. Among all the languages, JavaScript is probably the language where closure is mostly frequently used. In my opinion, the reason why closures are so widely used in Javascript lies in that Javascript does not have a mature OO system compared to other programming languages.

function a()
{
    var t = 1;
    function b()
    {
        console.log(++t);
    }
    return b;
}

x = a();
x();
x();

The output of this piece of Javascript code is “2” and “3”. As we can see, function a() returned another function. However, this function not only carries the information about itself, but also the context it lies in, i.e. the value of variable t.

In Apple’s programming language Swift, we have similar closures that act almost identical to Javascript’s.

import Foundation

func a() -> (Void -> Void){
    var t = 1;
    func b() -> Void{
        println(++t);
    }
    return b;
}

var x = a();
x();
x();

The result is also the same as Javascript’s, “2” and “3”. What about Python?

def a():
   t = 1;
   def b(t = t):
      t += 1;
      print(t);
   return b;

x = a();
x();
x();

There are a few notable differences here. First of all, the variable t defined in a() is not visible to b(), thus it must be passed through named argument. Secondly, the output is “2” and “2”. This is reasonable since the value of t is passed through an argument and changing the value of the argument will not affect its original value. However, python do support “real” closures. The trick is to change the t into a nonlocal variable.

def a():
   t = 1;
   def b():
      nonlocal t;
      t += 1;
      print(t);
   return b;

x = a();
x();
x();

Now we are finally there. Next up, Groovy.

def a()
{
    int t = 1;
    return { -> println(++t)};
}

x = a();
x();
x();

Since in Groovy, named functions cannot be defined in another function, we can only use unnamed function to do this. The output is “2” and “3”.

In Java, functions are not first-class members, thus we will never have terms like closure. A workaround for this is to use anonymous class. Here is an example.

public class Closure
{
   public static Function a()
   {
      final int t = 1;
      return new Function(){
         @Override
         public void call()
         {
            System.out.println(t + 1);
         }
      };
   }

   public static void main(String[] args)
   {
      Function x = a();
      x.call();
      x.call();
   }
}

interface Function
{
   public void call();
}

Rule No.1 for anonymous class is that you cannot change the value of the variables that exist in the stack context. In other words, the variables on the stack are all “final” to the inner class. If one want’s to change the value of it, it must be declared as a field of a Class, which will be stored in heap.

Android Support Library v22.1 轻度使用感受

前两天Google出了新的Support Library v22.1,这两天轻度使用了一下,贴点使用感受,慢慢更新

  1. Google又改Activity的基类了,原来是ActionbarActivity,现在是AppCompatActivity。很多逻辑也发生变化,之前是用Toolbar作为Actionbar的,现在也不需要了,直接自带Actionbar了,也是Material Design的设计,这个改动如果是从新写App的话挺好,如果是之前的老项目。。估计要改的吐血吧
  2. 各种Material Design的组件更新极其缓慢,这次确实又加了几个,但是设置不了深度,从用户的角度根本看不出来有多大变化。Annimation什么的通通没有,Google看这样子是不打算支持4.x用户了么?
  3. AlertDialog出了新的了,改下import就行了,好评。

发现一个有趣的东西:Docker

最近在写一个新的应用,后端用的是Nginx +  Python + Django + Gunicorn + Celery + libav,Celery又依赖RabbitMQ,为了让Celery和Gunicorn跑起来又用了Supervisor,东西太多配置又太繁琐,写代码调试和部署都是一个挑战,时间长了怕是配置文件和日志的location都要忘光光。我的工作环境是Mac和Windows,服务器则是Ubuntu或者Centos,像libav这种东西基本上算是Linux独有的,Mac虽然是*nix like但是毕竟不是Linux,想了半天最后决定放弃在Mac做Celery worker的调试,改在Ubuntu的服务器上用最原始的Log来分析调试,简直蛋疼到不行。

这两天在网上看到了Docker这个玩意,看起来确实不错:在Linux上有近乎Native的性能,在其它平台上则通过类似虚拟机的机制来构建运行环境,通过remote debug机制和IDE沟通,而部署则有点像拷贝虚拟机镜像:Docker的每一个优势都直击开发部署的痛点,下次部署的时候一定要用一下。

愿让我满怀期望的Docker,不要像Cygwin一样让我失望。

Overload vs Override, Java vs Groovy

Groovy is one of my favorite languages. It is simple yet powerful. More importantly, it can be integrated into Java stack easily, bringing us thousands of libraries that are ready to use.

One interesting thing about Java and Groovy is the difference in treating overload. A lot of scripting languages do not have function overloading. For example, in Python you need to use named parameters if you want to have some kinds of workaround for that. In JavaScript you need to check if a parameter is defined manually in the function, which brings complexity and lacks intuition. Same things happen to Matlab, where you have to check the number of arguments like the old C code:

int main(int argc, char** args);

Java has overloading, and it is reasonable that Groovy also has overloading. However, they treat overloading differently.

Java dispatch overloading during compile time. In other words, it is fixed and cannot be changed during run-time. However, as a dynamic language, Groovy dispatch overloading at run-time, which causes a different result in the following program.

//Java Code
public class TestJava
{
   public void print(Object a)
   {
      System.out.println("Object");
   }


   public void print(String a)
   {
      System.out.println("String");
   }

   public static void main(String[] args)
   {
      TestJava test = new TestJava();
      String s = "Hello World";
      Object o = s;
      test.print(s);
      test.print(o);
   }
}
//Groovy Code
class TestGroovy {
    public void print(Object a)
    {
        System.out.println("Object");
    }


    public void print(String a)
    {
        System.out.println("String");
    }

    public static void main(String[] args)
    {
        TestGroovy test = new TestGroovy();
        String s = "Hello World";
        Object o = s;
        test.print(s);
        test.print(o);
    }
}

These two pieces of code are identical to each other, except for the class name. However, the results are quite different. In Java, the output is String and Object, while in Groovy, the output is String and String.

What if we call the Java code in Groovy, and call the Groovy code in Java?

//Java Code
public class TestJava
{
   public void print(Object a)
   {
      System.out.println("Object");
   }

   public void print(String a)
   {
      System.out.println("String");
   }

   public static void main(String[] args)
   {
      String s = "Hello World";
      Object o = s;
      TestGroovy groovy = new TestGroovy();
      groovy.print(s);
      groovy.print(o);
   }
}
//Groovy Code
class TestGroovy {
    public void print(Object a)
    {
        System.out.println("Object");
    }


    public void print(String a)
    {
        System.out.println("String");
    }

    public static void main(String[] args)
    {
        String s = "Hello World";
        Object o = s;
        TestJava java = new TestJava();
        java.print(s);
        java.print(o);
    }
}

For the Java code, the output is String and Object, and for Groovy, String and String.

This experiment tells us the only deciding factor of which kind of dispatch method is being used is your code type that you are actually writing. If you are writing Java, not matter you are referring a code from Java or Groovy,  overloading functions are dispatched at compile time. If you are writing Groovy, overloading functions are dispatched at run time. This brings us a very essential problem: we need to be really careful with overloaded functions that come from Java library when we are writing Groovy code: it is the devil in the detail.

The good news is that, for function override, both Java and Groovy treat it in run-time and share the same behaviour.

改用CloudFlare的CDN并且启用了SSL

前段时间申请了StartSSL的免费SSL证书,但是由于Incapsula的免费版不支持SSL,也就一直没有部署。这两天看到Github的DDOS攻击事件,看到即使是一个毫不相关的网站,也有可能被别有用心的用来进行一些网络攻击,遂决定部署起来。

现在已经将CDN切换到了CloudFlare上,并且启用了Strict SSL,服务器端也设置了HTTPS的重定向,不过由于WordPress一直使用的是绝对地址来链接网站内容,所以还会有一些图片和视频的资源文件走HTTP,这个问题可能需要再过段时间想想怎么解决。

The DDOS attack against Github & HTTP Hijacking in China

It is well known that recently a large scale DDOS attack was carried out against Github. The obvious attacker: Great Fire Wall in China, did this by HTTP Hijacking.

A lot of Chinese websites use Baidu’s Analytic service to collect visiting information as an alternative to Google Analytics, which is not available in mainland China. The GFW hijacked the Baidu’s Analytic Javascript file of all in bounding requests, i.e. from outside China to inside China, and changed the content of it so that this piece of Javascript code will visit several certain Github pages every 2 seconds. This caused a heavy burden on Github’s servers and eventually became a DDOS attack in which GFW made use of millions of Chinese people who are outside China but visiting Chinese websites to achieve its goal.

HTTP hijacking is so common in China that it is not a surprise for me to hear that GFW eventually made use of this to attack Github. In China, every ISP is using HTTP hijacking to push ads to end users. In fact, some of the hijacking code is so ugly written that it will even hijack some API HTTP requests like Json response. It has been confirmed by some of the users of an open source android app which I contributed to. They reported this issue several days ago and such circumstances will only happen when using certain ISP service.

The reason why there are so many HTTP hijacking in China may lie in two reasons. First, the ISP market is not a free market and only some of nationally owned companies are allowed to provide Internet service. Second, few websites use HTTPS to deliver their contents, which made them vulnerable to MITM attacks.

 

搬瓦工已经没救了

一直以为搬瓦工最近速度越来越慢是因为港大的出口带宽吃不消了,今天有空在Digital Ocean上开了一个Singapore的VPS测试了一下,发现是搬瓦工的问题。

这个是新加坡到HostUs的:

Capture

这个是新加坡到搬瓦工的:

Capture2

这个是SFO到搬瓦工的:

Capture3

都是在西海岸,搬瓦工的跨洋线路慢成狗,西海岸内部连接倒是挺快但是对我没有半毛钱用处啊,再这样下去搬瓦工到期就可以扔掉了。

我的新玩具们

俗话说,”所有的男人都是孩子“。 即使已经不再是当初那个无忧无虑每天只知道玩耍的小男孩,我的玩心却依然还在。最近在网上买来了两个玩具,一个是Google Cardboard的华强北仿制版,另一个是PowerUp 2.0的仿制版。

  • Google Cardboard

Google Cardboard 是去年Google I/O大会时推出的一款廉价虚拟现实设备。之所以廉价,是因为它使用手机作为主要的显示和交互设备,而Cardboard本身就是几块硬纸板和两个凸透镜而已。

hero-home_2x

how-it-works_2x

至于我买的这个华强北山寨版,则是在Google Cardboard的原本设计上进行修改,使用塑料来构建机身,并且设计了三根松紧带来固定,镜片也可以调节焦距和瞳距,可谓是一个超级加强版。

IMG_20150223_171239

IMG_20150223_171504

构造和Google Cardboard基本一致,一边是有两个凸透镜,用于观测的孔,另一边则是用于防止手机的卡扣结构。把手机从这个位置放进去,就可以戴上这个玩意,在镜头这边进行观看了。那么看的时候呢,手机上显示的大致是这个玩意:

Screenshot_2015-03-09-13-01-30

左边是左眼看到的,右边是右眼看到的,两边的图像并不完全相同,就可以给人3D视觉。配合上手机里自带的陀螺仪,屏幕显示的内容可以根据头部的转动而变化,这就是沉浸式的虚拟现实体验了。不得不佩服Google的创意,把高大上的VR,用了一个这么小巧的廉价玩意就诠释的淋漓尽致。我的眼镜从淘宝上购入,不到100元。

实际体验上来说,效果还是相当不错的。我的手机分辨率只有720p,如果手机的分辨率能到1080p甚至2k,体验会更好。我觉得最有趣的应用还是Google Cardboard Demo里面的Youtube,那个视频墙出来的时候,只能用震撼两个字来形容。更重要的是,这个Youtube Demo是一个完全能拿来用的Demo,里面可以用语音进行搜索,搜索结果也会显示在视频墙上,视频本身虽然是2D的,但是营造出的氛围,让人仿佛独自置身于一间偌大的放映厅中静静欣赏。

  • PowerUp 2.0

PowerUp 2.0是一款电动纸飞机工具。纸飞机还能电动?当然,它是一款可以加装在纸飞机上电动发动机,让纸飞机可以飞的更高、更远、更久。准确的说来,这个玩意并不能算是山寨,因为它本身就是国内玩具厂商的杰作,只不过墙内开花墙外香,到了国外才被人发现、热捧。

71cuTHt8jxL._SL1500_

头部是一个电容,可以存储一些电量,尾部则是一个小的电动发动机,用于驱动飞机,中间的纸飞机则可以用A4纸自己叠。结构和原理都很简单,飞行的好坏则主要取决于折出来纸飞机结构的动力学优劣了。先放一个别人飞的视频,来自Youtube:

再放上两段我自己飞的视频:

 

 

虽然不及第一个视频里飞的那么高和远,但也是远超一般的纸飞机了!